THRIFT-2905 Cocoa compiler should have option to produce "modern" Objective-C
Client: Cocoa (ObjectiveC & Swift)
Author: Kevin Wooten <kevin@wooten.com>

This closes #539
diff --git a/lib/cocoa/src/TApplicationError.h b/lib/cocoa/src/TApplicationError.h
new file mode 100644
index 0000000..079881a
--- /dev/null
+++ b/lib/cocoa/src/TApplicationError.h
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#import "TProtocol.h"
+
+extern NSString *TApplicationErrorDomain;
+
+typedef NS_ENUM (int, TApplicationError) {
+  TApplicationErrorUnknown                = 0,
+  TApplicationErrorUnknownMethod          = 1,
+  TApplicationErrorInvalidMessageType     = 2,
+  TApplicationErrorWrongMethodName        = 3,
+  TApplicationErrorBadSequenceId          = 4,
+  TApplicationErrorMissingResult          = 5,
+  TApplicationErrorInternalError          = 6,
+  TApplicationErrorProtocolError          = 7,
+  TApplicationErrorInvalidTransform       = 8,
+  TApplicationErrorInvalidProtocol        = 9,
+  TApplicationErrorUnsupportedClientType  = 10,
+};
+
+
+extern NSString *TApplicationErrorNameKey;
+extern NSString *TApplicationErrorReasonKey;
+extern NSString *TApplicationErrorMethodKey;
+
+
+@interface NSError (TApplicationError)
+
+@property (readonly, copy) NSString *name;
+@property (readonly, copy) NSString *reason;
+
++(instancetype) errorWithType:(TApplicationError)type reason:(NSString *)reason;
+
++(instancetype) read:(id<TProtocol>)protocol;
+
+-(BOOL) write:(id<TProtocol>)outProtocol error:(NSError *__autoreleasing *)error;
+
+@end
diff --git a/lib/cocoa/src/TApplicationError.m b/lib/cocoa/src/TApplicationError.m
new file mode 100644
index 0000000..080bc0b
--- /dev/null
+++ b/lib/cocoa/src/TApplicationError.m
@@ -0,0 +1,231 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#import "TApplicationError.h"
+#import "TProtocolUtil.h"
+
+
+NSString *TApplicationErrorDomain = @"TApplicationErrorDomain";
+
+
+NSString *TApplicationErrorNameKey = @"name";
+NSString *TApplicationErrorReasonKey = @"reason";
+NSString *TApplicationErrorMethodKey = @"method";
+
+
+@implementation NSError (TApplicationError)
+
+-(NSString *) reason
+{
+  return self.userInfo[TApplicationErrorReasonKey];
+}
+
+-(NSString *) name
+{
+  return self.userInfo[TApplicationErrorNameKey];
+}
+
++(instancetype) errorWithType:(TApplicationError)type reason:(NSString *)reason
+{
+  NSString *name;
+  switch (type) {
+  case TApplicationErrorUnknownMethod:
+    name = @"Unknown Method";
+    break;
+
+  case TApplicationErrorInvalidMessageType:
+    name = @"Invalid Message Type";
+    break;
+
+  case TApplicationErrorWrongMethodName:
+    name = @"Wrong Method Name";
+    break;
+
+  case TApplicationErrorBadSequenceId:
+    name = @"Bad Sequence ID";
+    break;
+
+  case TApplicationErrorMissingResult:
+    name = @"Missing Result";
+    break;
+
+  case TApplicationErrorInternalError:
+    name = @"Internal Error";
+    break;
+
+  case TApplicationErrorProtocolError:
+    name = @"Protocol Error";
+    break;
+
+  case TApplicationErrorInvalidTransform:
+    name = @"Invalid Transform";
+    break;
+
+  case TApplicationErrorInvalidProtocol:
+    name = @"Invalid Protocol";
+    break;
+
+  case TApplicationErrorUnsupportedClientType:
+    name = @"Unsupported Client Type";
+    break;
+
+  default:
+    name = @"Unknown";
+    break;
+  }
+
+  NSDictionary *userInfo;
+  if (reason) {
+    userInfo = @{TApplicationErrorNameKey:name,
+                 TApplicationErrorReasonKey:reason};
+  }
+  else {
+    userInfo = @{TApplicationErrorNameKey:name};
+  }
+
+  return [NSError errorWithDomain:TApplicationErrorDomain
+                             code:type
+                         userInfo:userInfo];
+}
+
+
++(instancetype) read:(id<TProtocol>)protocol
+{
+  NSString *reason = nil;
+  SInt32 type = TApplicationErrorUnknown;
+  SInt32 fieldType;
+  SInt32 fieldID;
+
+  NSError *error;
+  if (![protocol readStructBeginReturningName:NULL error:&error]) {
+    return error;
+  }
+
+  while (true) {
+
+    if (![protocol readFieldBeginReturningName:NULL
+                                          type:&fieldType
+                                       fieldID:&fieldID
+                                         error:&error])
+    {
+      return error;
+    }
+
+    if (fieldType == TTypeSTOP) {
+      break;
+    }
+
+    switch (fieldID) {
+    case 1:
+      if (fieldType == TTypeSTRING) {
+        if (![protocol readString:&reason error:&error]) {
+          return error;
+        }
+      }
+      else {
+        if (![TProtocolUtil skipType:fieldType onProtocol:protocol error:&error]) {
+          return error;
+        }
+      }
+      break;
+
+    case 2:
+      if (fieldType == TTypeI32) {
+        if (![protocol readI32:&type error:&error]) {
+          return error;
+        }
+      }
+      else {
+        if (![TProtocolUtil skipType:fieldType onProtocol:protocol error:&error]) {
+          return error;
+        }
+      }
+      break;
+
+    default:
+      if (![TProtocolUtil skipType:fieldType onProtocol:protocol error:&error]) {
+        return error;
+      }
+      break;
+    }
+    if (![protocol readFieldEnd:&error]) {
+      return error;
+    }
+
+  }
+
+  if (![protocol readStructEnd:&error]) {
+    return error;
+  }
+
+  return [NSError errorWithType:type reason:reason];
+}
+
+
+-(BOOL) write:(id<TProtocol>)protocol error:(NSError *__autoreleasing *)error
+{
+  if (![protocol writeStructBeginWithName:@"TApplicationException" error:error]) {
+    return NO;
+  }
+
+  if (self.localizedDescription != nil) {
+    if (![protocol writeFieldBeginWithName:@"message"
+                                      type:TTypeSTRING
+                                   fieldID:1 error:error])
+    {
+      return NO;
+    }
+
+    if (![protocol writeString:self.localizedDescription error:error]) {
+      return NO;
+    }
+
+    if (![protocol writeFieldEnd:error]) {
+      return NO;
+    }
+  }
+
+  if (![protocol writeFieldBeginWithName:@"type"
+                                    type:TTypeI32
+                                 fieldID:2
+                                   error:error])
+  {
+    return NO;
+  }
+
+  if (![protocol writeI32:(SInt32)self.code error:error]) {
+    return NO;
+  }
+
+  if (![protocol writeFieldEnd:error]) {
+    return NO;
+  }
+
+  if (![protocol writeFieldStop:error]) {
+    return NO;
+  }
+
+  if (![protocol writeStructEnd:error]) {
+    return NO;
+  }
+
+  return YES;
+}
+
+@end
diff --git a/lib/cocoa/src/TApplicationException.h b/lib/cocoa/src/TApplicationException.h
deleted file mode 100644
index 7b027d6..0000000
--- a/lib/cocoa/src/TApplicationException.h
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#import "TException.h"
-#import "TProtocol.h"
-
-enum {
-  TApplicationException_UNKNOWN = 0,
-  TApplicationException_UNKNOWN_METHOD = 1,
-  TApplicationException_INVALID_MESSAGE_TYPE = 2,
-  TApplicationException_WRONG_METHOD_NAME = 3,
-  TApplicationException_BAD_SEQUENCE_ID = 4,
-  TApplicationException_MISSING_RESULT = 5,
-  TApplicationException_INTERNAL_ERROR = 6,
-  TApplicationException_PROTOCOL_ERROR = 7,
-  TApplicationException_INVALID_TRANSFORM = 8,
-  TApplicationException_INVALID_PROTOCOL = 9,
-  TApplicationException_UNSUPPORTED_CLIENT_TYPE = 10
-};
-
-// FIXME
-@interface TApplicationException : TException {
-  int mType;
-}
-
-+ (TApplicationException *) read: (id <TProtocol>) protocol;
-
-- (void) write: (id <TProtocol>) protocol;
-
-+ (TApplicationException *) exceptionWithType: (int) type
-                                       reason: (NSString *) message;
-
-@end
diff --git a/lib/cocoa/src/TApplicationException.m b/lib/cocoa/src/TApplicationException.m
deleted file mode 100644
index 974dfc5..0000000
--- a/lib/cocoa/src/TApplicationException.m
+++ /dev/null
@@ -1,146 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#import "TApplicationException.h"
-#import "TProtocolUtil.h"
-#import "TObjective-C.h"
-
-@implementation TApplicationException
-
-- (id) initWithType: (int) type
-             reason: (NSString *) reason
-{
-  mType = type;
-
-  NSString * name;
-  switch (type) {
-  case TApplicationException_UNKNOWN_METHOD:
-    name = @"Unknown method";
-    break;
-  case TApplicationException_INVALID_MESSAGE_TYPE:
-    name = @"Invalid message type";
-    break;
-  case TApplicationException_WRONG_METHOD_NAME:
-    name = @"Wrong method name";
-    break;
-  case TApplicationException_BAD_SEQUENCE_ID:
-    name = @"Bad sequence ID";
-    break;
-  case TApplicationException_MISSING_RESULT:
-    name = @"Missing result";
-    break;
-  case TApplicationException_INTERNAL_ERROR:
-    name = @"Internal error";
-    break;
-  case TApplicationException_PROTOCOL_ERROR:
-    name = @"Protocol error";
-    break;
-  case TApplicationException_INVALID_TRANSFORM:
-    name = @"Invalid transform";
-    break;
-  case TApplicationException_INVALID_PROTOCOL:
-    name = @"Invalid protocol";
-    break;
-  case TApplicationException_UNSUPPORTED_CLIENT_TYPE:
-    name = @"Unsupported client type";
-    break;
-  default:
-    name = @"Unknown";
-    break;
-  }
-
-  self = [super initWithName: name reason: reason userInfo: nil];
-  return self;
-}
-
-
-+ (TApplicationException *) read: (id <TProtocol>) protocol
-{
-  NSString * reason = nil;
-  int type = TApplicationException_UNKNOWN;
-  int fieldType;
-  int fieldID;
-
-  [protocol readStructBeginReturningName: NULL];
-
-  while (true) {
-    [protocol readFieldBeginReturningName: NULL
-              type: &fieldType
-              fieldID: &fieldID];
-    if (fieldType == TType_STOP) {
-      break;
-    }
-    switch (fieldID) {
-    case 1:
-      if (fieldType == TType_STRING) {
-        reason = [protocol readString];
-      } else {
-        [TProtocolUtil skipType: fieldType onProtocol: protocol];
-      }
-      break;
-    case 2:
-      if (fieldType == TType_I32) {
-        type = [protocol readI32];
-      } else {
-        [TProtocolUtil skipType: fieldType onProtocol: protocol];
-      }
-      break;
-    default:
-      [TProtocolUtil skipType: fieldType onProtocol: protocol];
-      break;
-    }
-    [protocol readFieldEnd];
-  }
-  [protocol readStructEnd];
-
-  return [TApplicationException exceptionWithType: type reason: reason];
-}
-
-
-- (void) write: (id <TProtocol>) protocol
-{
-  [protocol writeStructBeginWithName: @"TApplicationException"];
-
-  if ([self reason] != nil) {
-    [protocol writeFieldBeginWithName: @"message"
-                 type: TType_STRING
-                 fieldID: 1];
-    [protocol writeString: [self reason]];
-    [protocol writeFieldEnd];
-  }
-
-  [protocol writeFieldBeginWithName: @"type"
-               type: TType_I32
-               fieldID: 2];
-  [protocol writeI32: mType];
-  [protocol writeFieldEnd];
-
-  [protocol writeFieldStop];
-  [protocol writeStructEnd];
-}
-
-
-+ (TApplicationException *) exceptionWithType: (int) type
-                                      reason: (NSString *) reason
-{
-  return [[[TApplicationException alloc] initWithType: type
-                                         reason: reason] autorelease_stub];
-}
-
-@end
diff --git a/lib/cocoa/src/TBaseClient.h b/lib/cocoa/src/TBaseClient.h
index 12944b1..0f73aa0 100644
--- a/lib/cocoa/src/TBaseClient.h
+++ b/lib/cocoa/src/TBaseClient.h
@@ -18,13 +18,10 @@
  */
 
 #import "TProtocol.h"
-#import "TApplicationException.h"
+#import "TApplicationError.h"
 
-@interface TBaseClient : NSObject {
-    id <TProtocol> inProtocol;
-    id <TProtocol> outProtocol;
-}
+@interface TBaseClient : NSObject
 
-- (TApplicationException *)checkIncomingMessageException;
+-(NSError *) checkIncomingMessageException:(id<TProtocol>)protocol;
 
 @end
diff --git a/lib/cocoa/src/TBaseClient.m b/lib/cocoa/src/TBaseClient.m
index d15f9d3..249cae0 100644
--- a/lib/cocoa/src/TBaseClient.m
+++ b/lib/cocoa/src/TBaseClient.m
@@ -18,29 +18,34 @@
  */
 
 #import "TBaseClient.h"
-#import "TApplicationException.h"
-#import "TObjective-C.h"
+#import "TApplicationError.h"
+
+
+@interface TBaseClient ()
+@end
+
 
 @implementation TBaseClient
 
-- (void) dealloc
+-(NSError *) checkIncomingMessageException:(id<TProtocol>)inProtocol
 {
-    [inProtocol release_stub];
-    [outProtocol release_stub];
-    [super dealloc_stub];
-}
+  NSError *thriftError;
 
-- (TApplicationException *)checkIncomingMessageException
-{
-    int msgType = 0;
-    [inProtocol readMessageBeginReturningName: nil type: &msgType sequenceID: NULL];
-    if (msgType == TMessageType_EXCEPTION) {
-        TApplicationException * x = [TApplicationException read: inProtocol];
-        [inProtocol readMessageEnd];
-        return x;
-    }
-    
-    return nil;
+  SInt32 msgType = 0;
+  if (![inProtocol readMessageBeginReturningName:nil type:&msgType sequenceID:NULL error:&thriftError]) {
+    return thriftError;
+  }
+
+  if (msgType == TMessageTypeEXCEPTION) {
+
+    thriftError = [NSError read:inProtocol];
+
+    [inProtocol readMessageEnd:NULL];
+
+    return thriftError;
+  }
+
+  return nil;
 }
 
 @end
diff --git a/lib/cocoa/src/TBinary.swift b/lib/cocoa/src/TBinary.swift
new file mode 100644
index 0000000..c8a3660
--- /dev/null
+++ b/lib/cocoa/src/TBinary.swift
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import Foundation
+
+
+public struct TBinary : TSerializable {
+  
+  public static var thriftType : TType { return .STRING }
+  
+  private var storage : NSData
+  
+  public init() {
+    self.storage = NSData()
+  }
+  
+  public init(contentsOfFile file: String, options: NSDataReadingOptions = []) throws {
+    self.storage = try NSData(contentsOfFile: file, options: options)
+  }
+  
+  public init(contentsOfURL URL: NSURL, options: NSDataReadingOptions = []) throws {
+    self.storage = try NSData(contentsOfURL: URL, options: options)
+  }
+  
+  public init?(base64EncodedData base64Data: NSData, options: NSDataBase64DecodingOptions = []) {
+    guard let storage = NSData(base64EncodedData: base64Data, options: options) else {
+      return nil
+    }
+    self.storage = storage
+  }
+  
+  public init(data: NSData) {
+    self.storage = data
+  }
+  
+  public var length : Int {
+    return storage.length
+  }
+  
+  public var hashValue : Int {
+    return storage.hashValue
+  }
+  
+  public var bytes : UnsafePointer<Void> {
+    return storage.bytes
+  }
+  
+  public func getBytes(buffer: UnsafeMutablePointer<Void>, length: Int) {
+    storage.getBytes(buffer, length: length)
+  }
+  
+  public func getBytes(buffer: UnsafeMutablePointer<Void>, range: Range<Int>) {
+    storage.getBytes(buffer, range: NSRange(range))
+  }
+  
+  public func subBinaryWithRange(range: Range<Int>) -> TBinary {
+    return TBinary(data: storage.subdataWithRange(NSRange(range)))
+  }
+  
+  public func writeToFile(path: String, options: NSDataWritingOptions = []) throws {
+    try storage.writeToFile(path, options: options)
+  }
+  
+  public func writeToURL(url: NSURL, options: NSDataWritingOptions = []) throws {
+    try storage.writeToURL(url, options: options)
+  }
+  
+  public func rangeOfData(dataToFind data: NSData, options: NSDataSearchOptions, range: Range<Int>) -> Range<Int>? {
+    return storage.rangeOfData(data, options: options, range: NSRange(range)).toRange()
+  }
+  
+  public func enumerateByteRangesUsingBlock(block: (UnsafePointer<Void>, Range<Int>, inout Bool) -> Void) {
+    storage.enumerateByteRangesUsingBlock { bytes, range, stop in
+      var stopTmp = Bool(stop.memory)
+      block(bytes, range.toRange()!, &stopTmp)
+      stop.memory = ObjCBool(stopTmp)
+    }
+  }
+  
+  public static func readValueFromProtocol(proto: TProtocol) throws -> TBinary {
+    var data : NSData?
+    try proto.readBinary(&data)
+    return TBinary(data: data!)
+  }
+  
+  public static func writeValue(value: TBinary, toProtocol proto: TProtocol) throws {
+    try proto.writeBinary(value.storage)
+  }
+  
+}
+
+extension TBinary : CustomStringConvertible, CustomDebugStringConvertible {
+  
+  public var description : String {
+    return storage.description
+  }
+  
+  public var debugDescription : String {
+    return storage.debugDescription
+  }
+  
+}
+
+public func ==(lhs: TBinary, rhs: TBinary) -> Bool {
+  return lhs.storage == rhs.storage
+}
diff --git a/lib/cocoa/src/protocol/TProtocolException.h b/lib/cocoa/src/TEnum.swift
similarity index 83%
rename from lib/cocoa/src/protocol/TProtocolException.h
rename to lib/cocoa/src/TEnum.swift
index ad354fc..562a53a 100644
--- a/lib/cocoa/src/protocol/TProtocolException.h
+++ b/lib/cocoa/src/TEnum.swift
@@ -17,9 +17,15 @@
  * under the License.
  */
 
-#import "TException.h"
+import Foundation
 
-@interface TProtocolException : TException {
+
+public protocol TEnum : TSerializable {
+  
 }
 
-@end
+public extension TEnum {
+  
+  public static var thriftType : TType { return TType.I32 }
+  
+}
diff --git a/lib/cocoa/src/protocol/TProtocolException.m b/lib/cocoa/src/TError.h
similarity index 91%
rename from lib/cocoa/src/protocol/TProtocolException.m
rename to lib/cocoa/src/TError.h
index 681487a..abb72c7 100644
--- a/lib/cocoa/src/protocol/TProtocolException.m
+++ b/lib/cocoa/src/TError.h
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-#import "TProtocolException.h"
+#import <Foundation/Foundation.h>
 
-@implementation TProtocolException
-@end
+
+extern NSString *TErrorDomain;
diff --git a/lib/cocoa/src/protocol/TProtocolException.m b/lib/cocoa/src/TError.m
similarity index 91%
copy from lib/cocoa/src/protocol/TProtocolException.m
copy to lib/cocoa/src/TError.m
index 681487a..df7f92d 100644
--- a/lib/cocoa/src/protocol/TProtocolException.m
+++ b/lib/cocoa/src/TError.m
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-#import "TProtocolException.h"
+#import "TError.h"
 
-@implementation TProtocolException
-@end
+
+NSString *TErrorDomain = @"TErrorDomain";
diff --git a/lib/cocoa/src/TException.h b/lib/cocoa/src/TException.h
deleted file mode 100644
index e56f4fa..0000000
--- a/lib/cocoa/src/TException.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#import <Foundation/Foundation.h>
-
-@interface TException : NSException {
-}
-
-+ (id) exceptionWithName: (NSString *) name;
-
-+ (id) exceptionWithName: (NSString *) name
-                  reason: (NSString *) reason;
-
-+ (id) exceptionWithName: (NSString *) name
-                  reason: (NSString *) reason
-                   error: (NSError *) error;
-
-@end
diff --git a/lib/cocoa/src/TException.m b/lib/cocoa/src/TException.m
deleted file mode 100644
index 0160e3b..0000000
--- a/lib/cocoa/src/TException.m
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#import "TException.h"
-#import "TObjective-C.h"
-
-@implementation TException
-
-+ (id) exceptionWithName: (NSString *) name
-{
-  return [self exceptionWithName: name reason: @"unknown" error: nil];
-}
-
-
-+ (id) exceptionWithName: (NSString *) name
-                  reason: (NSString *) reason
-{
-  return [self exceptionWithName: name reason: reason error: nil];
-}
-
-
-+ (id) exceptionWithName: (NSString *) name
-                  reason: (NSString *) reason
-                   error: (NSError *) error
-{
-  NSDictionary * userInfo = nil;
-  if (error != nil) {
-    userInfo = [NSDictionary dictionaryWithObject: error forKey: @"error"];
-  }
-
-  return [super exceptionWithName: name
-                reason: reason
-                userInfo: userInfo];
-}
-
-
-- (NSString *) description
-{
-  NSMutableString * result = [NSMutableString stringWithString: [self name]];
-  [result appendFormat: @": %@", [self reason]];
-  if ([self userInfo] != nil) {
-    [result appendFormat: @"\n  userInfo = %@", [self userInfo]];
-  }
-
-  return result;
-}
-
-
-@end
diff --git a/lib/cocoa/src/TList.swift b/lib/cocoa/src/TList.swift
new file mode 100644
index 0000000..005bd81
--- /dev/null
+++ b/lib/cocoa/src/TList.swift
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import Foundation
+
+
+
+public struct TList<Element : TSerializable> : MutableCollectionType, Hashable, ArrayLiteralConvertible, TSerializable {
+  
+  public static var thriftType : TType { return .LIST }
+  
+  typealias Storage = Array<Element>
+
+  public typealias Index = Storage.Index
+  
+  private var storage = Storage()
+  
+  public var startIndex : Index {
+    return storage.startIndex
+  }
+  
+  public var endIndex : Index {
+    return storage.endIndex
+  }
+
+  public subscript (position: Index) -> Element {
+    get {
+      return storage[position]
+    }
+    set {
+      storage[position] = newValue
+    }
+  }
+  
+  public var hashValue : Int {
+    let prime = 31
+    var result = 1
+    for element in storage {
+      result = prime * result + element.hashValue
+    }
+    return result
+  }
+  
+  public init(arrayLiteral elements: Element...) {
+    self.storage = Storage(storage)
+  }
+  
+  public init() {
+    self.storage = Storage()
+  }
+  
+  public mutating func append(newElement: Element) {
+    self.storage.append(newElement)
+  }
+  
+  public mutating func appendContentsOf<C : CollectionType where C.Generator.Element == Element>(newstorage: C) {
+    self.storage.appendContentsOf(newstorage)
+  }
+  
+  public mutating func insert(newElement: Element, atIndex index: Int) {
+    self.storage.insert(newElement, atIndex: index)
+  }
+  
+  public mutating func insertContentsOf<C : CollectionType where C.Generator.Element == Element>(newElements: C, at index: Int) {
+    self.storage.insertContentsOf(newElements, at: index)
+  }
+  
+  public mutating func removeAll(keepCapacity keepCapacity: Bool = true) {
+    self.storage.removeAll(keepCapacity: keepCapacity)
+  }
+  
+  public mutating func removeAtIndex(index: Index) {
+    self.storage.removeAtIndex(index)
+  }
+  
+  public mutating func removeFirst(n: Int = 0) {
+    self.storage.removeFirst(n)
+  }
+  
+  public mutating func removeLast() -> Element {
+    return self.storage.removeLast()
+  }
+  
+  public mutating func removeRange(subRange: Range<Index>) {
+    self.storage.removeRange(subRange)
+  }
+  
+  public mutating func reserveCapacity(minimumCapacity: Int) {
+    self.storage.reserveCapacity(minimumCapacity)
+  }
+  
+  public static func readValueFromProtocol(proto: TProtocol) throws -> TList {
+    let (elementType, size) = try proto.readListBegin()
+    if elementType != Element.thriftType {
+      throw NSError(
+        domain: TProtocolErrorDomain,
+        code: Int(TProtocolError.InvalidData.rawValue),
+        userInfo: [TProtocolErrorExtendedErrorKey: NSNumber(int: TProtocolExtendedError.UnexpectedType.rawValue)])
+    }
+    var list = TList()
+    for _ in 0..<size {
+      let element = try Element.readValueFromProtocol(proto)
+      list.storage.append(element)
+    }
+    try proto.readListEnd()
+    return list
+  }
+  
+  public static func writeValue(value: TList, toProtocol proto: TProtocol) throws {
+    try proto.writeListBeginWithElementType(Element.thriftType, size: value.count)
+    for element in value.storage {
+      try Element.writeValue(element, toProtocol: proto)
+    }
+    try proto.writeListEnd()
+  }
+}
+
+extension TList : CustomStringConvertible, CustomDebugStringConvertible {
+  
+  public var description : String {
+    return storage.description
+  }
+  
+  public var debugDescription : String {
+    return storage.debugDescription
+  }
+  
+}
+
+public func ==<Element>(lhs: TList<Element>, rhs: TList<Element>) -> Bool {
+  return lhs.storage == rhs.storage
+}
diff --git a/lib/cocoa/src/TMap.swift b/lib/cocoa/src/TMap.swift
new file mode 100644
index 0000000..e96e747
--- /dev/null
+++ b/lib/cocoa/src/TMap.swift
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import Foundation
+
+
+public struct TMap<Key : TSerializable, Value : TSerializable> : CollectionType, DictionaryLiteralConvertible, TSerializable {
+  
+  public static var thriftType : TType { return .MAP }
+
+  typealias Storage = Dictionary<Key, Value>
+
+  public typealias Index = Storage.Index
+
+  public typealias Element = Storage.Element
+  
+  private var storage : Storage
+
+  public var startIndex : Index {
+    return storage.startIndex
+  }
+  
+  public var endIndex: Index {
+    return storage.endIndex
+  }
+
+  public var keys: LazyMapCollection<[Key : Value], Key> {
+    return storage.keys
+  }
+  
+  public var values: LazyMapCollection<[Key : Value], Value> {
+    return storage.values
+  }
+  
+  public init() {
+    storage = Storage()
+  }
+  
+  public init(dictionaryLiteral elements: (Key, Value)...) {
+    storage = Storage()
+    for (key, value) in elements {
+      storage[key] = value
+    }
+  }
+  
+  public init(minimumCapacity: Int) {
+    storage = Storage(minimumCapacity: minimumCapacity)
+  }
+  
+  public subscript (position: Index) -> Element {
+    get {
+      return storage[position]
+    }
+  }
+  
+  public func indexForKey(key: Key) -> Index? {
+    return storage.indexForKey(key)
+  }
+  
+  public subscript (key: Key) -> Value? {
+    get {
+      return storage[key]
+    }
+    set {
+      storage[key] = newValue
+    }
+  }
+
+  public mutating func updateValue(value: Value, forKey key: Key) -> Value? {
+    return updateValue(value, forKey: key)
+  }
+  
+  public mutating func removeAtIndex(index: DictionaryIndex<Key, Value>) -> (Key, Value) {
+    return removeAtIndex(index)
+  }
+  
+  public mutating func removeValueForKey(key: Key) -> Value? {
+    return storage.removeValueForKey(key)
+  }
+  
+  public mutating func removeAll(keepCapacity keepCapacity: Bool = false) {
+    storage.removeAll(keepCapacity: keepCapacity)
+  }
+
+  public var hashValue : Int {
+    let prime = 31
+    var result = 1
+    for (key, value) in storage {
+      result = prime * result + key.hashValue
+      result = prime * result + value.hashValue
+    }
+    return result
+  }
+  
+  public static func readValueFromProtocol(proto: TProtocol) throws -> TMap {
+    let (keyType, valueType, size) = try proto.readMapBegin()
+    if keyType != Key.thriftType || valueType != Value.thriftType {
+      throw NSError(
+        domain: TProtocolErrorDomain,
+        code: Int(TProtocolError.InvalidData.rawValue),
+        userInfo: [TProtocolErrorExtendedErrorKey: NSNumber(int: TProtocolExtendedError.UnexpectedType.rawValue)])
+    }
+    var map = TMap()
+    for _ in 0..<size {
+      let key = try Key.readValueFromProtocol(proto)
+      let value = try Value.readValueFromProtocol(proto)
+      map.storage[key] = value
+    }
+    try proto.readMapEnd()
+    return map
+  }
+  
+  public static func writeValue(value: TMap, toProtocol proto: TProtocol) throws {
+    try proto.writeMapBeginWithKeyType(Key.thriftType, valueType: Value.thriftType, size: value.count)
+    for (key, value) in value.storage {
+      try Key.writeValue(key, toProtocol: proto)
+      try Value.writeValue(value, toProtocol: proto)
+    }
+    try proto.writeMapEnd()
+  }
+  
+}
+
+
+extension TMap : CustomStringConvertible, CustomDebugStringConvertible {
+  
+  public var description : String {
+    return storage.description
+  }
+  
+  public var debugDescription : String {
+    return storage.debugDescription
+  }
+  
+}
+
+public func ==<Key, Value>(lhs: TMap<Key,Value>, rhs: TMap<Key, Value>) -> Bool {
+  if lhs.count != rhs.count {
+    return false
+  }
+  return lhs.storage == rhs.storage
+}
diff --git a/lib/cocoa/src/TObjective-C.h b/lib/cocoa/src/TObjective-C.h
deleted file mode 100644
index 9c0831d..0000000
--- a/lib/cocoa/src/TObjective-C.h
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-/*
- * TObjective-C.h is for supporting coexistence of both the ARC (Automatic 
- * Reference Counting) mode and the Non-ARC mode of Objective-C 
- * in the same source code.
- *
- *                  2011/11/14  HIRANO Satoshi (AIST, Japan)
- *
- * Before:
- *
- *    var = [aObject retain];
- *    [aObject release];
- *    [aObject autorelease];
- *    [super dealloc];
- *    CFFunction(obj);
- *
- * ARC and Non-ARC compatible:
- *
- *    #import "TObjective-C.h"
- *    var = [aObject retain_stub];
- *    [aObject release_stub];
- *    [aObject autorelease_stub];
- *    [super dealloc_stub];
- *    CFFunction(bridge_stub obj);
- *
- *    Don't use retain_stub for @property(retain).
- *    Use NSAutoreleasePool like this:
- *        #if __has_feature(objc_arc)
- *          @autoreleasepool {
- *              // code 
- *          }
- *        #else
- *          NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init...
- *          // code
- *          [pool release];
- *        #endif
- */
-
-
-#if !defined(retain_stub)
-#if __has_feature(objc_arc)
-#define retain_stub self
-#define autorelease_stub self
-#define release_stub self
-#define dealloc_stub self
-#define bridge_stub __bridge
-#else
-#define retain_stub retain
-#define autorelease_stub autorelease
-#define release_stub release
-#define dealloc_stub dealloc
-#define bridge_stub
-#endif
-#endif
diff --git a/lib/cocoa/src/TProcessor.h b/lib/cocoa/src/TProcessor.h
index 980be94..20c72e2 100644
--- a/lib/cocoa/src/TProcessor.h
+++ b/lib/cocoa/src/TProcessor.h
@@ -20,10 +20,16 @@
 #import <Foundation/Foundation.h>
 #import "TProtocol.h"
 
+NS_ASSUME_NONNULL_BEGIN
+
 
 @protocol TProcessor <NSObject>
 
-- (BOOL) processOnInputProtocol: (id <TProtocol>) inProtocol
-                 outputProtocol: (id <TProtocol>) outProtocol;
+-(BOOL) processOnInputProtocol:(id <TProtocol>)inProtocol
+                outputProtocol:(id <TProtocol>)outProtocol
+                         error:(NSError **)error;
 
 @end
+
+
+NS_ASSUME_NONNULL_END
diff --git a/lib/cocoa/src/TProcessorFactory.h b/lib/cocoa/src/TProcessorFactory.h
index 29d12b3..85020a5 100644
--- a/lib/cocoa/src/TProcessorFactory.h
+++ b/lib/cocoa/src/TProcessorFactory.h
@@ -20,8 +20,14 @@
 #import <Foundation/Foundation.h>
 #import "TProcessor.h"
 
+NS_ASSUME_NONNULL_BEGIN
+
+
 @protocol TProcessorFactory <NSObject>
 
-- (id<TProcessor>) processorForTransport: (id<TTransport>) transport;
+-(id<TProcessor>) processorForTransport:(id<TTransport>)transport;
 
 @end
+
+
+NS_ASSUME_NONNULL_END
diff --git a/lib/cocoa/src/TProtocol.swift b/lib/cocoa/src/TProtocol.swift
new file mode 100644
index 0000000..1775849
--- /dev/null
+++ b/lib/cocoa/src/TProtocol.swift
@@ -0,0 +1,190 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import Foundation
+
+
+public extension TProtocol {
+  
+  public func readMessageBegin() throws -> (String, TMessageType, Int) {
+    
+    var name : NSString?
+    var type : Int32 = -1
+    var sequenceID : Int32 = -1
+    
+    try readMessageBeginReturningName(&name, type: &type, sequenceID: &sequenceID)
+    
+    return (name as String!, TMessageType(rawValue: type)!, Int(sequenceID))
+  }
+  
+  public func writeMessageBeginWithName(name: String, type: TMessageType, sequenceID: Int) throws {
+    try writeMessageBeginWithName(name, type: type.rawValue, sequenceID: Int32(sequenceID))
+  }
+  
+  public func readStructBegin() throws -> (String?) {
+    
+    var name : NSString? = nil
+    
+    try readStructBeginReturningName(&name)
+    
+    return (name as String?)
+  }
+  
+  public func readFieldBegin() throws -> (String?, TType, Int) {
+    
+    var name : NSString? = nil
+    var type : Int32 = -1
+    var fieldID : Int32 = -1
+    
+    try readFieldBeginReturningName(&name, type: &type, fieldID: &fieldID)
+    
+    return (name as String?, TType(rawValue: type)!, Int(fieldID))
+  }
+  
+  public func writeFieldBeginWithName(name: String, type: TType, fieldID: Int) throws {
+    try writeFieldBeginWithName(name, type: type.rawValue, fieldID: Int32(fieldID))
+  }
+  
+  public func readMapBegin() throws -> (TType, TType, Int32) {
+    
+    var keyType : Int32 = -1
+    var valueType : Int32 = -1
+    var size : Int32 = 0
+    
+    try readMapBeginReturningKeyType(&keyType, valueType: &valueType, size: &size)
+    
+    return (TType(rawValue: keyType)!, TType(rawValue: valueType)!, size)
+  }
+  
+  public func writeMapBeginWithKeyType(keyType: TType, valueType: TType, size: Int) throws {
+    try writeMapBeginWithKeyType(keyType.rawValue, valueType: valueType.rawValue, size: Int32(size))
+  }
+  
+  public func readSetBegin() throws -> (TType, Int32) {
+    
+    var elementType : Int32 = -1
+    var size : Int32 = 0
+    
+    try readSetBeginReturningElementType(&elementType, size: &size)
+    
+    return (TType(rawValue: elementType)!, size)
+  }
+  
+  public func writeSetBeginWithElementType(elementType: TType, size: Int) throws {
+    try writeSetBeginWithElementType(elementType.rawValue, size: Int32(size))
+  }
+  
+  public func readListBegin() throws -> (TType, Int32) {
+    
+    var elementType : Int32 = -1
+    var size : Int32 = 0
+    
+    try readListBeginReturningElementType(&elementType, size: &size)
+    
+    return (TType(rawValue: elementType)!, size)
+  }
+  
+  public func writeListBeginWithElementType(elementType: TType, size: Int) throws {
+    try writeListBeginWithElementType(elementType.rawValue, size: Int32(size))
+  }
+  
+  public func writeFieldValue<T: TSerializable>(value: T, name: String, type: TType, id: Int32) throws {
+    try writeFieldBeginWithName(name, type: type.rawValue, fieldID: id)
+    try writeValue(value)
+    try writeFieldEnd()
+  }
+  
+  public func readValue<T: TSerializable>() throws -> T {
+    return try T.readValueFromProtocol(self)
+  }
+  
+  public func writeValue<T: TSerializable>(value: T) throws {
+    try T.writeValue(value, toProtocol: self)
+  }
+  
+  public func readResultMessageBegin() throws {
+    
+    let (_, type, _) = try readMessageBegin();
+    
+    if type == .EXCEPTION {
+      let x = try readException()
+      throw x
+    }
+    
+    return
+  }
+  
+  public func validateValue(value: Any?, named name: String) throws {
+    
+    if value == nil {
+      throw NSError(
+        domain: TProtocolErrorDomain,
+        code: Int(TProtocolError.Unknown.rawValue),
+        userInfo: [TProtocolErrorFieldNameKey: name])
+    }
+    
+  }
+  
+  public func readException() throws -> ErrorType {
+    
+    var reason : String?
+    var type = TApplicationError.Unknown
+    
+    try readStructBegin()
+    
+    fields: while (true) {
+      
+      let (_, fieldType, fieldID) = try readFieldBegin()
+      
+      switch (fieldID, fieldType) {
+      case (_, .STOP):
+        break fields
+        
+      case (1, .STRING):
+        reason = try readValue() as String
+        
+      case (2, .I32):
+        let typeVal = try readValue() as Int32
+        if let tmp = TApplicationError(rawValue: typeVal) {
+          type = tmp
+        }
+        
+      case let (_, unknownType):
+        try skipType(unknownType)
+      }
+      
+      try readFieldEnd()
+    }
+    
+    try readStructEnd()
+    
+    return NSError(type:type, reason:reason ?? "")
+  }
+  
+  public func writeExceptionForMessageName(name: String, sequenceID: Int, ex: NSError) throws {
+    try writeMessageBeginWithName(name, type: .EXCEPTION, sequenceID: sequenceID)
+    try ex.write(self)
+    try writeMessageEnd()
+  }
+  
+  public func skipType(type: TType) throws {
+    try TProtocolUtil.skipType(type.rawValue, onProtocol: self)
+  }
+  
+}
diff --git a/lib/cocoa/src/TSerializable.swift b/lib/cocoa/src/TSerializable.swift
new file mode 100644
index 0000000..3fdfd41
--- /dev/null
+++ b/lib/cocoa/src/TSerializable.swift
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import Foundation
+
+
+public protocol TSerializable : Hashable {
+  
+  static var thriftType : TType { get }
+  
+  init()
+  
+  static func readValueFromProtocol(proto: TProtocol) throws -> Self
+  
+  static func writeValue(value: Self, toProtocol proto: TProtocol) throws
+  
+}
+
+
+
+infix operator ?== {}
+
+public func ?==<T: TSerializable>(lhs: T?, rhs: T?) -> Bool {
+  if let l = lhs, r = rhs {
+    return l == r
+  }
+  return lhs == rhs
+}
+
+public func ?==<T: TSerializable>(lhs: T, rhs: T) -> Bool {
+  return lhs == rhs
+}
+
+
+
+extension Bool : TSerializable {
+  
+  public static let thriftType = TType.BOOL
+  
+  public static func readValueFromProtocol(proto: TProtocol) throws -> Bool {
+    var value : ObjCBool = false
+    try proto.readBool(&value)
+    return value.boolValue
+  }
+  
+  public static func writeValue(value: Bool, toProtocol proto: TProtocol) throws {
+    try proto.writeBool(value)
+  }
+  
+}
+
+extension Int8 : TSerializable {
+  
+  public static let thriftType = TType.BYTE
+  
+  public static func readValueFromProtocol(proto: TProtocol) throws -> Int8 {
+    var value = UInt8()
+    try proto.readByte(&value)
+    return Int8(value)
+  }
+  
+  public static func writeValue(value: Int8, toProtocol proto: TProtocol) throws {
+    try proto.writeByte(UInt8(value))
+  }
+  
+}
+
+extension Int16 : TSerializable {
+  
+  public static let thriftType = TType.I16
+  
+  public static func readValueFromProtocol(proto: TProtocol) throws -> Int16 {
+    var value = Int16()
+    try proto.readI16(&value)
+    return value
+  }
+  
+  public static func writeValue(value: Int16, toProtocol proto: TProtocol) throws {
+    try proto.writeI16(value)
+  }
+  
+}
+
+extension Int : TSerializable {
+  
+  public static let thriftType = TType.I32
+  
+  public static func readValueFromProtocol(proto: TProtocol) throws -> Int {
+    var value = Int32()
+    try proto.readI32(&value)
+    return Int(value)
+  }
+  
+  public static func writeValue(value: Int, toProtocol proto: TProtocol) throws {
+    try proto.writeI32(Int32(value))
+  }
+  
+}
+
+extension Int32 : TSerializable {
+  
+  public static let thriftType = TType.I32
+  
+  public static func readValueFromProtocol(proto: TProtocol) throws -> Int32 {
+    var value = Int32()
+    try proto.readI32(&value)
+    return value
+  }
+  
+  public static func writeValue(value: Int32, toProtocol proto: TProtocol) throws {
+    try proto.writeI32(value)
+  }
+  
+}
+
+extension Int64 : TSerializable {
+  
+  public static let thriftType = TType.I64
+  
+  public static func readValueFromProtocol(proto: TProtocol) throws -> Int64 {
+    var value = Int64()
+    try proto.readI64(&value)
+    return value
+  }
+  
+  public static func writeValue(value: Int64, toProtocol proto: TProtocol) throws {
+    try proto.writeI64(value)
+  }
+  
+}
+
+extension Double : TSerializable {
+  
+  public static let thriftType = TType.DOUBLE
+  
+  public static func readValueFromProtocol(proto: TProtocol) throws -> Double {
+    var value = Double()
+    try proto.readDouble(&value)
+    return value
+  }
+  
+  public static func writeValue(value: Double, toProtocol proto: TProtocol) throws {
+    try proto.writeDouble(value)
+  }
+  
+}
+
+extension String : TSerializable {
+  
+  public static let thriftType = TType.STRING
+  
+  public static func readValueFromProtocol(proto: TProtocol) throws -> String {
+    var value : NSString?
+    try proto.readString(&value)
+    return value as! String
+  }
+  
+  public static func writeValue(value: String, toProtocol proto: TProtocol) throws {
+    try proto.writeString(value)
+  }
+  
+}
diff --git a/lib/cocoa/src/TSet.swift b/lib/cocoa/src/TSet.swift
new file mode 100644
index 0000000..85833e5
--- /dev/null
+++ b/lib/cocoa/src/TSet.swift
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import Foundation
+
+
+public struct TSet<Element : TSerializable> : CollectionType, ArrayLiteralConvertible, TSerializable {
+  
+  public static var thriftType : TType { return .SET }
+  
+  public typealias Index = Storage.Index
+  
+  typealias Storage = Set<Element>
+  
+  private var storage : Storage
+  
+  public init() {
+    storage = Storage()
+  }
+  
+  public init(arrayLiteral elements: Element...) {
+    storage = Storage(elements)
+  }
+  
+  public init<S : SequenceType where S.Generator.Element == Element>(_ sequence: S) {
+    storage = Storage(sequence)    
+  }
+  
+  public var startIndex : Index { return storage.startIndex }
+  
+  public var endIndex : Index { return storage.endIndex }
+  
+  public mutating func insert(member: Element) {
+    return storage.insert(member)
+  }
+  
+  public mutating func remove(element: Element) -> Element? {
+    return storage.remove(element)
+  }
+  
+  public mutating func removeAll(keepCapacity keepCapacity: Bool = false) {
+    return storage.removeAll(keepCapacity: keepCapacity)
+  }
+  
+  public mutating func removeAtIndex(index: SetIndex<Element>) -> Element {
+    return storage.removeAtIndex(index)
+  }
+  
+  public subscript (position: SetIndex<Element>) -> Element {
+    return storage[position]
+  }
+  
+  public func union(other: TSet) -> TSet {
+    return TSet(storage.union(other))
+  }
+  
+  public func intersect(other: TSet) -> TSet {
+    return TSet(storage.intersect(other))
+  }
+  
+  public func exclusiveOr(other: TSet) -> TSet {
+    return TSet(storage.exclusiveOr(other))
+  }
+  
+  public func subtract(other: TSet) -> TSet {
+    return TSet(storage.subtract(other))
+  }
+  
+  public mutating func intersectInPlace(other: TSet) {
+    storage.intersectInPlace(other)
+  }
+
+  public mutating func exclusiveOrInPlace(other: TSet) {
+    storage.exclusiveOrInPlace(other)
+  }
+
+  public mutating func subtractInPlace(other: TSet) {
+    storage.subtractInPlace(other)
+  }  
+
+  public func isSubsetOf(other: TSet) -> Bool {
+    return storage.isSubsetOf(other)
+  }
+
+  public func isDisjointWith(other: TSet) -> Bool {
+    return storage.isDisjointWith(other)
+  }
+  
+  public func isSupersetOf(other: TSet) -> Bool {
+    return storage.isSupersetOf(other)
+  }
+
+  public var isEmpty: Bool { return storage.isEmpty }
+
+  public var hashValue : Int {
+    let prime = 31
+    var result = 1
+    for element in storage {
+      result = prime * result + element.hashValue
+    }
+    return result
+  }
+  
+  public static func readValueFromProtocol(proto: TProtocol) throws -> TSet {
+    let (elementType, size) = try proto.readSetBegin()
+    if elementType != Element.thriftType {
+      throw NSError(
+        domain: TProtocolErrorDomain,
+        code: Int(TProtocolError.InvalidData.rawValue),
+        userInfo: [TProtocolErrorExtendedErrorKey: NSNumber(int: elementType.rawValue)])
+    }
+    var set = TSet()
+    for _ in 0..<size {
+      let element = try Element.readValueFromProtocol(proto)
+      set.storage.insert(element)
+    }
+    try proto.readSetEnd()
+    return set
+  }
+  
+  public static func writeValue(value: TSet, toProtocol proto: TProtocol) throws {
+    try proto.writeSetBeginWithElementType(Element.thriftType, size: value.count)
+    for element in value.storage {
+      try Element.writeValue(element, toProtocol: proto)
+    }
+    try proto.writeSetEnd()
+  }
+  
+}
+
+extension TSet : CustomStringConvertible, CustomDebugStringConvertible {
+  
+  public var description : String {
+    return storage.description
+  }
+  
+  public var debugDescription : String {
+    return storage.debugDescription
+  }
+  
+}
+
+public func ==<Element>(lhs: TSet<Element>, rhs: TSet<Element>) -> Bool {
+  return lhs.storage == rhs.storage
+}
diff --git a/lib/cocoa/src/TSharedProcessorFactory.h b/lib/cocoa/src/TSharedProcessorFactory.h
index cf4a462..c75fad1 100644
--- a/lib/cocoa/src/TSharedProcessorFactory.h
+++ b/lib/cocoa/src/TSharedProcessorFactory.h
@@ -20,8 +20,9 @@
 #import <Foundation/Foundation.h>
 #import "TProcessorFactory.h"
 
-@interface TSharedProcessorFactory : NSObject <TProcessorFactory> {
-  id<TProcessor> mSharedProcessor;
-}
-- (id) initWithSharedProcessor: (id<TProcessor>) sharedProcessor;
+
+@interface TSharedProcessorFactory : NSObject <TProcessorFactory>
+
+-(id) initWithSharedProcessor:(id<TProcessor>)sharedProcessor;
+
 @end
diff --git a/lib/cocoa/src/TSharedProcessorFactory.m b/lib/cocoa/src/TSharedProcessorFactory.m
index a0007c0..3d55f47 100644
--- a/lib/cocoa/src/TSharedProcessorFactory.m
+++ b/lib/cocoa/src/TSharedProcessorFactory.m
@@ -19,34 +19,31 @@
 
 
 #import "TSharedProcessorFactory.h"
-#import "TObjective-C.h"
+
+
+@interface TSharedProcessorFactory ()
+
+@property(strong, nonatomic) id<TProcessor> sharedProcessor;
+
+@end
 
 
 @implementation TSharedProcessorFactory
 
 
-- (id) initWithSharedProcessor: (id<TProcessor>) sharedProcessor
+-(id) initWithSharedProcessor:(id<TProcessor>)sharedProcessor
 {
   self = [super init];
-  if (!self) {
-    return nil;
+  if (self) {
+    _sharedProcessor = sharedProcessor;
   }
-  
-  mSharedProcessor = [sharedProcessor retain_stub];
+
   return self;
 }
 
-
-- (void) dealloc
+-(id<TProcessor>) processorForTransport:(id<TTransport>)transport
 {
-  [mSharedProcessor release_stub];
-  [super dealloc_stub];
-}
-
-
-- (id<TProcessor>) processorForTransport: (id<TTransport>) transport
-{
-  return [[mSharedProcessor retain_stub] autorelease_stub];
+  return _sharedProcessor;
 }
 
 @end
diff --git a/lib/cocoa/src/protocol/TProtocolException.m b/lib/cocoa/src/TStruct.swift
similarity index 82%
copy from lib/cocoa/src/protocol/TProtocolException.m
copy to lib/cocoa/src/TStruct.swift
index 681487a..cea72e7 100644
--- a/lib/cocoa/src/protocol/TProtocolException.m
+++ b/lib/cocoa/src/TStruct.swift
@@ -17,7 +17,15 @@
  * under the License.
  */
 
-#import "TProtocolException.h"
+import Foundation
 
-@implementation TProtocolException
-@end
+
+public protocol TStruct : TSerializable {
+}
+
+
+public extension TStruct {
+  
+  public static var thriftType : TType { return TType.STRUCT }
+  
+}
diff --git a/lib/cocoa/src/protocol/TBase.h b/lib/cocoa/src/protocol/TBase.h
index 33037ef..b31061e 100644
--- a/lib/cocoa/src/protocol/TBase.h
+++ b/lib/cocoa/src/protocol/TBase.h
@@ -26,16 +26,21 @@
 /**
  * De-serialize object from the given input protocol
  *
- * @param input protocol used for reading 
+ * @param input protocol used for reading
  */
-- (void) read: (id <TProtocol>) inProtocol;
+-(BOOL) read:(id <TProtocol>)inProtocol error:(NSError **)error;
 
 /**
  * Serialize object to the given protocol
  *
  * @param buf output protocol used for writing
  */
-- (void) write: (id <TProtocol>) outProtocol;
+-(BOOL) write:(id <TProtocol>)outProtocol error:(NSError **)error;
+
+
+/**
+ * Validate required fields
+ */
+-(BOOL) validate:(NSError *__autoreleasing *)__thriftError;
 
 @end
-
diff --git a/lib/cocoa/src/protocol/TBinaryProtocol.h b/lib/cocoa/src/protocol/TBinaryProtocol.h
index 9a73730..bb90fad 100644
--- a/lib/cocoa/src/protocol/TBinaryProtocol.h
+++ b/lib/cocoa/src/protocol/TBinaryProtocol.h
@@ -21,31 +21,29 @@
 #import "TTransport.h"
 #import "TProtocolFactory.h"
 
+NS_ASSUME_NONNULL_BEGIN
 
-@interface TBinaryProtocol : NSObject <TProtocol> {
-  id <TTransport> mTransport;
-  BOOL mStrictRead;
-  BOOL mStrictWrite;
-  int32_t mMessageSizeLimit;
-}
 
-- (id) initWithTransport: (id <TTransport>) transport;
+@interface TBinaryProtocol : NSObject <TProtocol>
 
-- (id) initWithTransport: (id <TTransport>) transport
-              strictRead: (BOOL) strictRead
-             strictWrite: (BOOL) strictWrite;
+@property (assign, nonatomic) UInt32 messageSizeLimit;
 
-- (int32_t) messageSizeLimit;
-- (void) setMessageSizeLimit: (int32_t) sizeLimit;
+-(id) initWithTransport:(id <TTransport>)transport;
+
+-(id) initWithTransport:(id <TTransport>)transport
+             strictRead:(BOOL)strictRead
+            strictWrite:(BOOL)strictWrite;
+
+@end;
+
+
+@interface TBinaryProtocolFactory : NSObject <TProtocolFactory>
+
++(TBinaryProtocolFactory *) sharedFactory;
+
+-(TBinaryProtocol *) newProtocolOnTransport:(id <TTransport>)transport;
 
 @end
 
 
-@interface TBinaryProtocolFactory : NSObject <TProtocolFactory> {
-}
-
-+ (TBinaryProtocolFactory *) sharedFactory;
-
-- (TBinaryProtocol *) newProtocolOnTransport: (id <TTransport>) transport;
-
-@end
+NS_ASSUME_NONNULL_END
\ No newline at end of file
diff --git a/lib/cocoa/src/protocol/TBinaryProtocol.m b/lib/cocoa/src/protocol/TBinaryProtocol.m
index 847c723..1f9e57a 100644
--- a/lib/cocoa/src/protocol/TBinaryProtocol.m
+++ b/lib/cocoa/src/protocol/TBinaryProtocol.m
@@ -18,63 +18,20 @@
  */
 
 #import "TBinaryProtocol.h"
-#import "TProtocolException.h"
-#import "TObjective-C.h"
+#import "TProtocolError.h"
 
-/* In the modern protocol, version is stored in the high half of an int32.
- * The low half contains type info. */
-static const uint16_t VERSION_1 = 0x8001;
 
-NS_INLINE size_t
-CheckedCastInt32ToSizeT(int32_t size)
-{
-  if (size < 0) {
-    NSString *reason = [NSString stringWithFormat:
-                        @"%s: refusing to read data with negative size: %"PRId32,
-                        __func__, size];
-    @throw [TProtocolException
-            exceptionWithName: @"TProtocolException"
-            reason: reason];
-  }
-  size_t checkedSize = (size_t)size;
-  return checkedSize;
-}
+static SInt32 VERSION_1 = 0x80010000;
+static SInt32 VERSION_MASK = 0xffff0000;
 
-NS_INLINE int32_t
-CheckedCastSizeTToInt32(size_t size)
-{
-  if (size > INT32_MAX) {
-    NSString *reason = [NSString stringWithFormat:
-                        @"%s: data size exceeds values representable by a 32-bit signed integer: %zu",
-                        __func__, size];
-    @throw [TProtocolException
-            exceptionWithName: @"TProtocolException"
-            reason: reason];
-  }
-  int32_t checkedSize = (int32_t)size;
-  return checkedSize;
-}
 
-NS_INLINE uint8_t
-CheckedCastIntToUInt8(int size)
-{
-  if (size > UINT8_MAX) {
-    NSString *reason = [NSString stringWithFormat:
-                        @"%s: data size exceeds values representable by a 8-bit unsigned integer: %d",
-                        __func__, size];
-    @throw [TProtocolException
-            exceptionWithName: @"TProtocolException"
-            reason: reason];
-  }
-  uint8_t checkedSize = (uint8_t)size;
-  return checkedSize;
-}
+static TBinaryProtocolFactory *gSharedFactory = nil;
 
-static TBinaryProtocolFactory * gSharedFactory = nil;
 
 @implementation TBinaryProtocolFactory
 
-+ (TBinaryProtocolFactory *) sharedFactory {
++(TBinaryProtocolFactory *) sharedFactory
+{
   if (gSharedFactory == nil) {
     gSharedFactory = [[TBinaryProtocolFactory alloc] init];
   }
@@ -82,363 +39,553 @@
   return gSharedFactory;
 }
 
-- (TBinaryProtocol *) newProtocolOnTransport: (id <TTransport>) transport {
-  return [[TBinaryProtocol alloc] initWithTransport: transport];
+-(NSString *) protocolName
+{
+  return @"binary";
+}
+
+-(TBinaryProtocol *) newProtocolOnTransport:(id <TTransport>)transport
+{
+  return [[TBinaryProtocol alloc] initWithTransport:transport];
 }
 
 @end
 
 
+@interface TBinaryProtocol ()
+
+@property(strong, nonatomic) id <TTransport> transport;
+
+@property(assign, nonatomic) BOOL strictRead;
+@property(assign, nonatomic) BOOL strictWrite;
+
+@property(strong, nonatomic) NSString *currentMessageName;
+@property(strong, nonatomic) NSString *currentFieldName;
+
+@end
+
 
 @implementation TBinaryProtocol
 
-- (id) initWithTransport: (id <TTransport>) transport
+-(id) initWithTransport:(id <TTransport>)aTransport
 {
-  return [self initWithTransport: transport strictRead: NO strictWrite: YES];
+  return [self initWithTransport:aTransport strictRead:NO strictWrite:YES];
 }
 
-- (id) initWithTransport: (id <TTransport>) transport
-              strictRead: (BOOL) strictRead
-             strictWrite: (BOOL) strictWrite
+-(id) initWithTransport:(id <TTransport>)transport
+             strictRead:(BOOL)strictRead
+            strictWrite:(BOOL)strictWrite
 {
   self = [super init];
-  mTransport = [transport retain_stub];
-  mStrictRead = strictRead;
-  mStrictWrite = strictWrite;
+  if (self) {
+    _transport = transport;
+    _strictRead = strictRead;
+    _strictWrite = strictWrite;
+  }
   return self;
 }
 
-
-- (int32_t) messageSizeLimit
+-(id <TTransport>) transport
 {
-  return mMessageSizeLimit;
+  return _transport;
 }
 
-
-- (void) setMessageSizeLimit: (int32_t) sizeLimit
+-(NSString *) readStringBody:(int)size error:(NSError **)error
 {
-  mMessageSizeLimit = sizeLimit;
-}
-
-
-- (void) dealloc
-{
-  [mTransport release_stub];
-  [super dealloc_stub];
-}
-
-
-- (id <TTransport>) transport
-{
-  return mTransport;
-}
-
-
-- (NSString *) readStringBody: (int) rawSize
-{
-  size_t size = CheckedCastInt32ToSizeT(rawSize);
-  char * buffer = malloc(size+1);
-  if (!buffer) {
-    @throw [TProtocolException exceptionWithName: @"TProtocolException"
-                                          reason: [NSString stringWithFormat: @"Unable to allocate memory in %s, size: %zu",
-                                                   __PRETTY_FUNCTION__,
-                                                   size]];;
+  NSMutableData *data = [NSMutableData dataWithLength:size];
+  if (!data) {
+    PROTOCOL_ERROR(nil, Unknown, @"Unable to allocate %d bytes", size);
   }
-  [mTransport readAll: (uint8_t *) buffer offset: 0 length: size];
-  buffer[size] = 0;
-  NSString * result = [NSString stringWithUTF8String: buffer];
-  free(buffer);
-  return result;
+
+  if (![_transport readAll:data.mutableBytes offset:0 length:size error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(nil, error, @"Transport read failed");
+  }
+
+  return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
 }
 
 
-- (void) readMessageBeginReturningName: (NSString **) name
-                                  type: (int *) type
-                            sequenceID: (int *) sequenceID
+-(BOOL) readMessageBeginReturningName:(NSString **)name
+                                 type:(SInt32 *)type
+                           sequenceID:(SInt32 *)sequenceID
+                                error:(NSError *__autoreleasing *)error
 {
-  int32_t size = [self readI32];
+  SInt32 size;
+  if (![self readI32:&size error:error]) {
+    return NO;
+  }
+  ;
+
   if (size < 0) {
-    /* Version (unsigned) is stored in the high halfword. */
-    uint16_t version = (size >> 16) & 0xFFFF;
+    int version = size & VERSION_MASK;
     if (version != VERSION_1) {
-      NSString *reason = [NSString stringWithFormat:
-                          @"%s: Expected version %"PRIu16", instead found: %"PRIu16,
-                          __func__, VERSION_1, version];
-      @throw [TProtocolException exceptionWithName: @"TProtocolException"
-                                 reason: reason];
+      PROTOCOL_ERROR(NO, BadVersion, @"Bad message version");
     }
     if (type != NULL) {
       *type = size & 0x00FF;
     }
-    NSString * messageName = [self readString];
+    NSString *messageName;
+    if (![self readString:&messageName error:error]) {
+      return NO;
+    }
+    if (name != nil) {
+      *name = messageName;
+    }
+  }
+  else {
+
+    if (_strictRead) {
+      PROTOCOL_ERROR(NO, InvalidData, @"Missing message version, old client?");
+    }
+
+    if (_messageSizeLimit > 0 && size > _messageSizeLimit) {
+      PROTOCOL_ERROR(NO, SizeLimit, @"Message exceeeds size limit of %d", (int)size);
+    }
+
+    NSString *messageName = [self readStringBody:size error:error];
+    if (!messageName) {
+      return NO;
+    }
+
     if (name != NULL) {
       *name = messageName;
     }
-    int seqID = [self readI32];
-    if (sequenceID != NULL) {
-      *sequenceID = seqID;
+
+    UInt8 messageType;
+    if (![self readByte:&messageType error:error]) {
+      return NO;
     }
-  } else {
-    if (mStrictRead) {
-      @throw [TProtocolException exceptionWithName: @"TProtocolException"
-                                 reason: @"Missing version in readMessageBegin, old client?"];
-    }
-    if ([self messageSizeLimit] > 0 && size > [self messageSizeLimit]) {
-      @throw [TProtocolException exceptionWithName: @"TProtocolException"
-                                            reason: [NSString stringWithFormat: @"Message too big.  Size limit is: %d Message size is: %d",
-                                                     mMessageSizeLimit,
-                                                     size]];
-    }
-    NSString * messageName = [self readStringBody: size];
-    if (name != NULL) {
-      *name = messageName;
-    }
-    int messageType = [self readByte];
+
     if (type != NULL) {
       *type = messageType;
     }
-    int seqID = [self readI32];
-    if (sequenceID != NULL) {
-      *sequenceID = seqID;
-    }
   }
+
+  SInt32 seqID;
+  if (![self readI32:&seqID error:error]) {
+    return NO;
+  }
+  if (sequenceID != NULL) {
+    *sequenceID = seqID;
+  }
+
+  return YES;
 }
 
 
-- (void) readMessageEnd {}
-
-
-- (void) readStructBeginReturningName: (NSString **) name
+-(BOOL) readMessageEnd:(NSError *__autoreleasing *)error
 {
-  if (name != NULL) {
-    *name = nil;
-  }
+  return YES;
 }
 
 
-- (void) readStructEnd {}
-
-
-- (void) readFieldBeginReturningName: (NSString **) name
-                                type: (int *) fieldType
-                             fieldID: (int *) fieldID
+-(BOOL) readStructBeginReturningName:(NSString *__autoreleasing *)name error:(NSError *__autoreleasing *)error
 {
-  if (name != NULL) {
+  return YES;
+}
+
+
+-(BOOL) readStructEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+
+
+-(BOOL) readFieldBeginReturningName:(NSString *__autoreleasing *)name
+                               type:(SInt32 *)fieldType
+                            fieldID:(SInt32 *)fieldID
+                              error:(NSError *__autoreleasing *)error
+{
+  if (name != nil) {
     *name = nil;
   }
-  int ft = [self readByte];
+
+  UInt8 ft;
+  if (![self readByte:&ft error:error]) {
+    return NO;
+  }
   if (fieldType != NULL) {
     *fieldType = ft;
   }
-  if (ft != TType_STOP) {
-    int fid = [self readI16];
+  if (ft != TTypeSTOP) {
+    SInt16 fid;
+    if (![self readI16:&fid error:error]) {
+      return NO;
+    }
     if (fieldID != NULL) {
       *fieldID = fid;
     }
   }
+  return YES;
 }
 
 
-- (void) readFieldEnd {}
-
-
-- (int32_t) readI32
+-(BOOL) readFieldEnd:(NSError *__autoreleasing *)error
 {
-  uint8_t i32rd[4];
-  [mTransport readAll: i32rd offset: 0 length: 4];
-  return
+  return YES;
+}
+
+
+-(BOOL) readString:(NSString *__autoreleasing *)value error:(NSError *__autoreleasing *)error
+{
+  SInt32 size;
+  if (![self readI32:&size error:error]) {
+    return NO;
+  }
+
+  NSString *string = [self readStringBody:size error:error];
+  if (!string) {
+    return NO;
+  }
+
+  *value = string;
+
+  return YES;
+}
+
+
+-(BOOL) readBool:(BOOL *)value error:(NSError *__autoreleasing *)error
+{
+  UInt8 byte;
+  if (![self readByte:&byte error:error]) {
+    return NO;
+  }
+
+  *value = byte == 1;
+
+  return YES;
+}
+
+
+-(BOOL) readByte:(UInt8 *)value error:(NSError *__autoreleasing *)error
+{
+  UInt8 buff[1];
+  if (![_transport readAll:buff offset:0 length:1 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport read failed");
+  }
+
+  *value = buff[0];
+
+  return YES;
+}
+
+
+-(BOOL) readI16:(SInt16 *)value error:(NSError *__autoreleasing *)error
+{
+  UInt8 buff[2];
+  if (![_transport readAll:buff offset:0 length:2 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport read failed");
+  }
+
+  *value =
+    ((SInt16)(buff[0] & 0xff) << 8) |
+    ((SInt16)(buff[1] & 0xff));
+
+  return YES;
+}
+
+
+-(BOOL) readI32:(SInt32 *)value error:(NSError *__autoreleasing *)error
+{
+  UInt8 i32rd[4];
+  if (![_transport readAll:i32rd offset:0 length:4 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport read failed");
+  }
+
+  *value =
     ((i32rd[0] & 0xff) << 24) |
     ((i32rd[1] & 0xff) << 16) |
     ((i32rd[2] & 0xff) <<  8) |
     ((i32rd[3] & 0xff));
+
+  return YES;
 }
 
 
-- (NSString *) readString
+-(BOOL) readI64:(SInt64 *)value error:(NSError *__autoreleasing *)error
 {
-  int32_t size = [self readI32];
-  return [self readStringBody: size];
+  UInt8 buff[8];
+  if (![_transport readAll:buff offset:0 length:8 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport read failed");
+  }
+
+  *value =
+    ((SInt64)(buff[0] & 0xff) << 56) |
+    ((SInt64)(buff[1] & 0xff) << 48) |
+    ((SInt64)(buff[2] & 0xff) << 40) |
+    ((SInt64)(buff[3] & 0xff) << 32) |
+    ((SInt64)(buff[4] & 0xff) << 24) |
+    ((SInt64)(buff[5] & 0xff) << 16) |
+    ((SInt64)(buff[6] & 0xff) <<  8) |
+    ((SInt64)(buff[7] & 0xff));
+
+  return YES;
 }
 
 
-- (BOOL) readBool
-{
-  return [self readByte] == 1;
-}
-
-- (uint8_t) readByte
-{
-  uint8_t myByte;
-  [mTransport readAll: &myByte offset: 0 length: 1];
-  return myByte;
-}
-
-- (short) readI16
-{
-  uint8_t buff[2];
-  [mTransport readAll: buff offset: 0 length: 2];
-  return (short)
-    (((buff[0] & 0xff) << 8) |
-     ((buff[1] & 0xff)));
-}
-
-- (int64_t) readI64
-{
-  uint8_t i64rd[8];
-  [mTransport readAll: i64rd offset: 0 length: 8];
-  return
-    ((int64_t)(i64rd[0] & 0xff) << 56) |
-    ((int64_t)(i64rd[1] & 0xff) << 48) |
-    ((int64_t)(i64rd[2] & 0xff) << 40) |
-    ((int64_t)(i64rd[3] & 0xff) << 32) |
-    ((int64_t)(i64rd[4] & 0xff) << 24) |
-    ((int64_t)(i64rd[5] & 0xff) << 16) |
-    ((int64_t)(i64rd[6] & 0xff) <<  8) |
-    ((int64_t)(i64rd[7] & 0xff));
-}
-
-- (double) readDouble
+-(BOOL) readDouble:(double *)value error:(NSError *__autoreleasing *)error
 {
   // FIXME - will this get us into trouble on PowerPC?
-  int64_t ieee754 = [self readI64];
-  return *((double *) &ieee754);
+  return [self readI64:(SInt64 *)value error:error];
 }
 
 
-- (NSData *) readBinary
+-(BOOL) readBinary:(NSData *__autoreleasing *)value error:(NSError *__autoreleasing *)error
 {
-  int32_t size = [self readI32];
-  size_t binarySize = CheckedCastInt32ToSizeT(size);
-  uint8_t * buff = malloc(binarySize);
-  if (buff == NULL) {
-    @throw [TProtocolException
-             exceptionWithName: @"TProtocolException"
-             reason: [NSString stringWithFormat: @"Out of memory.  Unable to allocate %d bytes trying to read binary data.",
-                               size]];
+  SInt32 size;
+  if (![self readI32:&size error:error]) {
+    return NO;
   }
-  [mTransport readAll: buff offset: 0 length: binarySize];
-  return [NSData dataWithBytesNoCopy: buff length: binarySize];
+
+  NSMutableData *data = [NSMutableData dataWithLength:size];
+  if (!data) {
+    PROTOCOL_ERROR(NO, Unknown, @"Unable to allocate %d bytes", (int)size);
+  }
+
+  if (![_transport readAll:data.mutableBytes offset:0 length:size error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport read failed");
+  }
+
+  *value = data;
+
+  return YES;
 }
 
 
-- (void) readMapBeginReturningKeyType: (int *) keyType
-                            valueType: (int *) valueType
-                                 size: (int *) size
+-(BOOL) readMapBeginReturningKeyType:(SInt32 *)keyType
+                           valueType:(SInt32 *)valueType
+                                size:(SInt32 *)size
+                               error:(NSError *__autoreleasing *)error
 {
-  int kt = [self readByte];
-  int vt = [self readByte];
-  int s = [self readI32];
+  UInt8 kt;
+  if (![self readByte:&kt error:error]) {
+    return NO;
+  }
+
+  UInt8 vt;
+  if (![self readByte:&vt error:error]) {
+    return NO;
+  }
+
+  SInt32 s;
+  if (![self readI32:&s error:error]) {
+    return NO;
+  }
+
   if (keyType != NULL) {
     *keyType = kt;
   }
+
   if (valueType != NULL) {
     *valueType = vt;
   }
+
   if (size != NULL) {
     *size = s;
   }
+
+  return YES;
 }
 
-- (void) readMapEnd {}
 
-
-- (void) readSetBeginReturningElementType: (int *) elementType
-                                     size: (int *) size
+-(BOOL) readMapEnd:(NSError *__autoreleasing *)error
 {
-  int et = [self readByte];
-  int s = [self readI32];
+  return YES;
+}
+
+
+-(BOOL) readSetBeginReturningElementType:(SInt32 *)elementType
+                                    size:(SInt32 *)size
+                                   error:(NSError *__autoreleasing *)error
+{
+  UInt8 et;
+  if (![self readByte:&et error:error]) {
+    return NO;
+  }
+
+  SInt32 s;
+  if (![self readI32:&s error:error]) {
+    return NO;
+  }
+
   if (elementType != NULL) {
     *elementType = et;
   }
+
   if (size != NULL) {
     *size = s;
   }
+
+  return YES;
 }
 
 
-- (void) readSetEnd {}
-
-
-- (void) readListBeginReturningElementType: (int *) elementType
-                                      size: (int *) size
+-(BOOL) readSetEnd:(NSError *__autoreleasing *)error
 {
-  int et = [self readByte];
-  int s = [self readI32];
+  return YES;
+}
+
+
+-(BOOL) readListBeginReturningElementType:(SInt32 *)elementType
+                                     size:(SInt32 *)size
+                                    error:(NSError *__autoreleasing *)error
+{
+  UInt8 et;
+  if (![self readByte:&et error:error]) {
+    return NO;
+  }
+
+  SInt32 s;
+  if (![self readI32:&s error:error]) {
+    return NO;
+  }
+
   if (elementType != NULL) {
     *elementType = et;
   }
+
   if (size != NULL) {
     *size = s;
   }
+
+  return YES;
 }
 
 
-- (void) readListEnd {}
-
-
-- (void) writeByte: (uint8_t) value
+-(BOOL) readListEnd:(NSError *__autoreleasing *)error
 {
-  [mTransport write: &value offset: 0 length: 1];
+  return YES;
 }
 
 
-- (void) writeMessageBeginWithName: (NSString *) name
-                              type: (int) messageType
-                        sequenceID: (int) sequenceID
+
+-(BOOL) writeMessageBeginWithName:(NSString *)name
+                             type:(SInt32)messageType
+                       sequenceID:(SInt32)sequenceID
+                            error:(NSError *__autoreleasing *)error
 {
-  if (mStrictWrite) {
-    int version = (VERSION_1 << 16) | messageType;
-    [self writeI32: version];
-    [self writeString: name];
-    [self writeI32: sequenceID];
-  } else {
-    [self writeString: name];
-    [self writeByte: CheckedCastIntToUInt8(messageType)];
-    [self writeI32: sequenceID];
+  if (_strictWrite) {
+
+    int version = VERSION_1 | messageType;
+
+    if (![self writeI32:version error:error]) {
+      return NO;
+    }
+
+    if (![self writeString:name error:error]) {
+      return NO;
+    }
+
+    if (![self writeI32:sequenceID error:error]) {
+      return NO;
+    }
   }
+  else {
+
+    if (![self writeString:name error:error]) {
+      return NO;
+    }
+
+    if (![self writeByte:messageType error:error]) {
+      return NO;
+    }
+
+    if (![self writeI32:sequenceID error:error]) {
+      return NO;
+    }
+  }
+
+  _currentMessageName = name;
+
+  return YES;
 }
 
 
-- (void) writeMessageEnd {}
-
-
-- (void) writeStructBeginWithName: (NSString *) name {}
-
-
-- (void) writeStructEnd {}
-
-
-- (void) writeFieldBeginWithName: (NSString *) name
-                            type: (int) fieldType
-                         fieldID: (int) fieldID
+-(BOOL) writeMessageEnd:(NSError *__autoreleasing *)error
 {
-  [self writeByte: CheckedCastIntToUInt8(fieldType)];
-  [self writeI16: CheckedCastIntToUInt8(fieldID)];
+  _currentMessageName = nil;
+  return YES;
 }
 
 
-- (void) writeI32: (int32_t) value
+-(BOOL) writeStructBeginWithName:(NSString *)name
+                           error:(NSError *__autoreleasing *)error
 {
-  uint8_t buff[4];
+  return YES;
+}
+
+
+-(BOOL) writeStructEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+
+
+-(BOOL) writeFieldBeginWithName:(NSString *)name
+                           type:(SInt32)fieldType
+                        fieldID:(SInt32)fieldID
+                          error:(NSError *__autoreleasing *)error
+{
+  if (![self writeByte:fieldType error:error]) {
+    return NO;
+  }
+
+  if (![self writeI16:fieldID error:error]) {
+    return NO;
+  }
+
+  return YES;
+}
+
+
+-(BOOL) writeBool:(BOOL)value error:(NSError *__autoreleasing *)error
+{
+  return [self writeByte:(value ? 1 : 0) error:error];
+}
+
+
+-(BOOL) writeByte:(UInt8)value error:(NSError *__autoreleasing *)error
+{
+  if (![_transport write:&value offset:0 length:1 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
+  }
+  return YES;
+}
+
+
+-(BOOL) writeI16:(short)value error:(NSError *__autoreleasing *)error
+{
+  UInt8 buff[2];
+  buff[0] = 0xff & (value >> 8);
+  buff[1] = 0xff & value;
+
+  if (![_transport write:buff offset:0 length:2 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
+  }
+
+  return YES;
+}
+
+
+-(BOOL) writeI32:(SInt32)value error:(NSError *__autoreleasing *)error
+{
+  UInt8 buff[4];
   buff[0] = 0xFF & (value >> 24);
   buff[1] = 0xFF & (value >> 16);
   buff[2] = 0xFF & (value >> 8);
   buff[3] = 0xFF & value;
-  [mTransport write: buff offset: 0 length: 4];
-}
 
-- (void) writeI16: (short) value
-{
-  uint8_t buff[2];
-  buff[0] = 0xff & (value >> 8);
-  buff[1] = 0xff & value;
-  [mTransport write: buff offset: 0 length: 2];
+  if (![_transport write:buff offset:0 length:4 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
+  }
+
+  return YES;
 }
 
 
-- (void) writeI64: (int64_t) value
+-(BOOL) writeI64:(SInt64)value error:(NSError *__autoreleasing *)error
 {
-  uint8_t buff[8];
+  UInt8 buff[8];
   buff[0] = 0xFF & (value >> 56);
   buff[1] = 0xFF & (value >> 48);
   buff[2] = 0xFF & (value >> 40);
@@ -447,84 +594,147 @@
   buff[5] = 0xFF & (value >> 16);
   buff[6] = 0xFF & (value >> 8);
   buff[7] = 0xFF & value;
-  [mTransport write: buff offset: 0 length: 8];
+
+  if (![_transport write:buff offset:0 length:8 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
+  }
+
+  return YES;
 }
 
-- (void) writeDouble: (double) value
+
+-(BOOL) writeDouble:(double)value error:(NSError *__autoreleasing *)error
 {
-  // spit out IEEE 754 bits - FIXME - will this get us in trouble on
-  // PowerPC?
-  [self writeI64: *((int64_t *) &value)];
+  // FIXME - will this get us in trouble on PowerPC?
+  if (![self writeI64:*(SInt64 *)&value error:error]) {
+    return NO;
+  }
+
+  return YES;
 }
 
 
-- (void) writeString: (NSString *) value
+-(BOOL) writeString:(NSString *)value error:(NSError *__autoreleasing *)error
 {
   if (value != nil) {
-    const char * utf8Bytes = [value UTF8String];
-    size_t length = strlen(utf8Bytes);
-    int32_t size = CheckedCastSizeTToInt32(length);
-    [self writeI32: size];
-    [mTransport write: (uint8_t *) utf8Bytes offset: 0 length: length];
-  } else {
+
+    const char *utf8Bytes = [value UTF8String];
+
+    SInt32 length = (SInt32)strlen(utf8Bytes);
+    if (![self writeI32:length error:error]) {
+      return NO;
+    }
+
+    if (![_transport write:(UInt8 *)utf8Bytes offset:0 length:(int)length error:error]) {
+      PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
+    }
+
+  }
+  else {
+
     // instead of crashing when we get null, let's write out a zero
     // length string
-    [self writeI32: 0];
+    if (![self writeI32:0 error:error]) {
+      return NO;
+    }
+
   }
+
+  return YES;
 }
 
 
-- (void) writeBinary: (NSData *) data
+-(BOOL) writeBinary:(NSData *)data error:(NSError *__autoreleasing *)error
 {
-  int32_t size = CheckedCastSizeTToInt32([data length]);
-  [self writeI32: size];
-  [mTransport write: [data bytes] offset: 0 length: [data length]];
-}
+  if (![self writeI32:(SInt32)data.length error:error]) {
+    return NO;
+  }
 
-- (void) writeFieldStop
-{
-  [self writeByte: TType_STOP];
+  if (![_transport write:data.bytes offset:0 length:(UInt32)data.length error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
+  }
+
+  return YES;
 }
 
 
-- (void) writeFieldEnd {}
-
-
-- (void) writeMapBeginWithKeyType: (int) keyType
-                        valueType: (int) valueType
-                             size: (int) size
+-(BOOL) writeFieldStop:(NSError *__autoreleasing *)error
 {
-  [self writeByte: CheckedCastIntToUInt8(keyType)];
-  [self writeByte: CheckedCastIntToUInt8(valueType)];
-  [self writeI32: size];
+  if (![self writeByte:TTypeSTOP error:error]) {
+    return NO;
+  }
+
+  return YES;
 }
 
-- (void) writeMapEnd {}
 
-
-- (void) writeSetBeginWithElementType: (int) elementType
-                                 size: (int) size
+-(BOOL) writeFieldEnd:(NSError *__autoreleasing *)error
 {
-  [self writeByte: CheckedCastIntToUInt8(elementType)];
-  [self writeI32: size];
+  return YES;
 }
 
-- (void) writeSetEnd {}
 
-
-- (void) writeListBeginWithElementType: (int) elementType
-                                  size: (int) size
+-(BOOL) writeMapBeginWithKeyType:(SInt32)keyType
+                       valueType:(SInt32)valueType
+                            size:(SInt32)size
+                           error:(NSError *__autoreleasing *)error
 {
-  [self writeByte: CheckedCastIntToUInt8(elementType)];
-  [self writeI32: size];
+  if (![self writeByte:keyType error:error]) {
+    return NO;
+  }
+  if (![self writeByte:valueType error:error]) {
+    return NO;
+  }
+  if (![self writeI32:(int)size error:error]) {
+    return NO;
+  }
+  return YES;
 }
 
-- (void) writeListEnd {}
 
-
-- (void) writeBool: (BOOL) value
+-(BOOL) writeMapEnd:(NSError *__autoreleasing *)error
 {
-  [self writeByte: (value ? 1 : 0)];
+  return YES;
+}
+
+
+-(BOOL) writeSetBeginWithElementType:(SInt32)elementType
+                                size:(SInt32)size
+                               error:(NSError *__autoreleasing *)error
+{
+  if (![self writeByte:elementType error:error]) {
+    return NO;
+  }
+  if (![self writeI32:size error:error]) {
+    return NO;
+  }
+  return YES;
+}
+
+
+-(BOOL) writeSetEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+
+
+-(BOOL) writeListBeginWithElementType:(SInt32)elementType
+                                 size:(SInt32)size
+                                error:(NSError *__autoreleasing *)error
+{
+  if (![self writeByte:elementType error:error]) {
+    return NO;
+  }
+  if (![self writeI32:size error:error]) {
+    return NO;
+  }
+  return YES;
+}
+
+
+-(BOOL) writeListEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
 }
 
 @end
diff --git a/lib/cocoa/src/protocol/TCompactProtocol.h b/lib/cocoa/src/protocol/TCompactProtocol.h
index 3c9195c..3f6accc 100644
--- a/lib/cocoa/src/protocol/TCompactProtocol.h
+++ b/lib/cocoa/src/protocol/TCompactProtocol.h
@@ -21,16 +21,22 @@
 #import "TTransport.h"
 #import "TProtocolFactory.h"
 
+NS_ASSUME_NONNULL_BEGIN
+
+
 @interface TCompactProtocol : NSObject <TProtocol>
 
-- (id) initWithTransport: (id <TTransport>) transport;
+-(id) initWithTransport:(id <TTransport>)transport;
 
 @end
 
 @interface TCompactProtocolFactory : NSObject <TProtocolFactory>
 
-+ (TCompactProtocolFactory *) sharedFactory;
++(TCompactProtocolFactory *) sharedFactory;
 
-- (TCompactProtocol *) newProtocolOnTransport: (id <TTransport>) transport;
+-(TCompactProtocol *) newProtocolOnTransport:(id <TTransport>)transport;
 
 @end
+
+
+NS_ASSUME_NONNULL_END
\ No newline at end of file
diff --git a/lib/cocoa/src/protocol/TCompactProtocol.m b/lib/cocoa/src/protocol/TCompactProtocol.m
index 45b0ef3..9b0ebb2 100644
--- a/lib/cocoa/src/protocol/TCompactProtocol.m
+++ b/lib/cocoa/src/protocol/TCompactProtocol.m
@@ -18,14 +18,13 @@
  */
 
 #import "TCompactProtocol.h"
-#import "TObjective-C.h"
-#import "TProtocolException.h"
+#import "TProtocolError.h"
 
-static const uint8_t COMPACT_PROTOCOL_ID = 0x82;
-static const uint8_t COMPACT_VERSION = 1;
-static const uint8_t COMPACT_VERSION_MASK = 0x1F; // 0001 1111
-static const uint8_t COMPACT_TYPE_MASK = 0xE0; // 1110 0000
-static const uint8_t COMPACT_TYPE_BITS = 0x07; // 0000 0111
+static const UInt8 COMPACT_PROTOCOL_ID = 0x82;
+static const UInt8 COMPACT_VERSION = 1;
+static const UInt8 COMPACT_VERSION_MASK = 0x1F; // 0001 1111
+static const UInt8 COMPACT_TYPE_MASK = 0xE0; // 1110 0000
+static const UInt8 COMPACT_TYPE_BITS = 0x07; // 0000 0111
 static const int COMPACT_TYPE_SHIFT_AMOUNT = 5;
 
 enum {
@@ -46,322 +45,442 @@
 
 @implementation TCompactProtocolFactory
 
-+ (TCompactProtocolFactory *) sharedFactory
++(TCompactProtocolFactory *) sharedFactory
 {
-  static TCompactProtocolFactory * gSharedFactory = nil;
+  static TCompactProtocolFactory *gSharedFactory = nil;
   if (gSharedFactory == nil) {
     gSharedFactory = [[TCompactProtocolFactory alloc] init];
   }
-  
+
   return gSharedFactory;
 }
 
-- (TCompactProtocol *) newProtocolOnTransport: (id <TTransport>) transport
+-(NSString *) protocolName
 {
-  return [[TCompactProtocol alloc] initWithTransport: transport];
+  return @"compact";
+}
+
+-(TCompactProtocol *) newProtocolOnTransport:(id <TTransport>)transport
+{
+  return [[TCompactProtocol alloc] initWithTransport:transport];
 }
 
 @end
 
-@implementation TCompactProtocol {
-  NSMutableArray * lastField;
-  short lastFieldId;
-  id <TTransport> mTransport;
-  
-  NSString * boolFieldName;
-  NSNumber * boolFieldType;
-  NSNumber * boolFieldId;
-  NSNumber * booleanValue;
-}
 
-- (id) init
+@interface TCompactProtocol ()
+
+@property(strong, nonatomic) id <TTransport> transport;
+
+@property(strong, nonatomic) NSMutableArray *lastField;
+@property(assign, nonatomic) short lastFieldId;
+
+@property(strong, nonatomic) NSString *boolFieldName;
+@property(strong, nonatomic) NSNumber *boolFieldType;
+@property(strong, nonatomic) NSNumber *boolFieldId;
+@property(strong, nonatomic) NSNumber *booleanValue;
+
+@property(strong, nonatomic) NSString *currentMessageName;
+
+@end
+
+
+@implementation TCompactProtocol
+
+-(id) init
 {
   self = [super init];
-  
+
   if (self != nil) {
-    lastField = [[NSMutableArray alloc] init];
+    _lastField = [[NSMutableArray alloc] init];
   }
-  
+
   return self;
 }
 
-- (id) initWithTransport: (id <TTransport>) transport
+-(id) initWithTransport:(id <TTransport>)aTransport
 {
   self = [self init];
-  
+
   if (self != nil) {
-    mTransport = [transport retain_stub];
+    _transport = aTransport;
   }
-  
+
   return self;
 }
 
-- (void) dealloc
+-(id <TTransport>) transport
 {
-  [lastField release_stub];
-  [mTransport release_stub];
-  [boolFieldName release_stub];
-  [boolFieldType release_stub];
-  [boolFieldId release_stub];
-  [booleanValue release_stub];
-  
-  [super dealloc_stub];
+  return _transport;
 }
 
-- (id <TTransport>) transport
+-(BOOL) writeByteDirect:(UInt8)n error:(NSError *__autoreleasing *)error
 {
-  return mTransport;
+  if (![_transport write:(UInt8 *)&n offset:0 length:1 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
+  }
+  return YES;
 }
 
-- (void) writeByteDirect: (int8_t) n
+-(BOOL) writeVarint32:(UInt32)n error:(NSError *__autoreleasing *)error
 {
-  [mTransport write: (uint8_t *)&n offset: 0 length: 1];
-}
+  UInt8 i32buf[5] = {0};
+  UInt32 idx = 0;
 
-- (void)writeVarint32: (uint32_t) n
-{
-  uint8_t i32buf[5] = {0};
-  uint32_t idx = 0;
-  
   while (true) {
     if ((n & ~0x7F) == 0) {
-      i32buf[idx++] = (uint8_t)n;
+      i32buf[idx++] = (UInt8)n;
       break;
-    } else {
-      i32buf[idx++] = (uint8_t)((n & 0x7F) | 0x80);
+    }
+    else {
+      i32buf[idx++] = (UInt8)((n & 0x7F) | 0x80);
       n >>= 7;
     }
   }
-  
-  [mTransport write: i32buf offset: 0 length: idx];
+
+  if (![_transport write:i32buf offset:0 length:idx error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
+  }
+
+  return YES;
 }
 
-- (void) writeMessageBeginWithName: (NSString *) name
-                              type: (int) messageType
-                        sequenceID: (int) sequenceID
+-(BOOL) writeMessageBeginWithName:(NSString *)name
+                             type:(SInt32)messageType
+                       sequenceID:(SInt32)sequenceID
+                            error:(NSError *__autoreleasing *)error
 {
-  [self writeByteDirect: COMPACT_PROTOCOL_ID];
-  [self writeByteDirect: (uint8_t)((COMPACT_VERSION & COMPACT_VERSION_MASK) |
-                                   ((((uint32_t)messageType) << COMPACT_TYPE_SHIFT_AMOUNT) & COMPACT_TYPE_MASK))];
-  [self writeVarint32: (uint32_t)sequenceID];
-  [self writeString: name];
+  if (![self writeByteDirect:COMPACT_PROTOCOL_ID error:error]) {
+    return NO;
+  }
+  if (![self writeByteDirect:(UInt8)((COMPACT_VERSION & COMPACT_VERSION_MASK) |
+                                     ((((UInt32)messageType) << COMPACT_TYPE_SHIFT_AMOUNT) & COMPACT_TYPE_MASK)) error:error])
+  {
+    return NO;
+  }
+  if (![self writeVarint32:(UInt32)sequenceID error:error]) {
+    return NO;
+  }
+  if (![self writeString:name error:error]) {
+    return NO;
+  }
+
+  _currentMessageName = name;
+
+  return YES;
 }
 
-- (void) writeStructBeginWithName: (NSString *) name
+-(BOOL) writeStructBeginWithName:(NSString *)name error:(NSError *__autoreleasing *)error
 {
-  [lastField addObject: [NSNumber numberWithShort: lastFieldId]];
-  lastFieldId = 0;
+  [_lastField addObject:@(_lastFieldId)];
+  _lastFieldId = 0;
+  return YES;
 }
 
-- (void) writeStructEnd
+-(BOOL) writeStructEnd:(NSError *__autoreleasing *)error
 {
-  lastFieldId = [[lastField lastObject] shortValue];
-  [lastField removeLastObject];
+  _lastFieldId = [_lastField.lastObject shortValue];
+  [_lastField removeLastObject];
+  return YES;
 }
 
-- (void) writeFieldBeginWithName: (NSString *) name
-                            type: (int) fieldType
-                         fieldID: (int) fieldID
+-(BOOL) writeFieldBeginWithName:(NSString *)name
+                           type:(SInt32)fieldType
+                        fieldID:(SInt32)fieldID
+                          error:(NSError *__autoreleasing *)error
 {
-  if (fieldType == TType_BOOL) {
-    boolFieldName = [name copy];
-    boolFieldType = [[NSNumber numberWithInt: fieldType] retain_stub];
-    boolFieldId = [[NSNumber numberWithInt: fieldID] retain_stub];
-  } else {
-    [self writeFieldBeginInternalWithName: name
-                                     type: fieldType
-                                  fieldID: fieldID
-                             typeOverride: 0xFF];
+  if (fieldType == TTypeBOOL) {
+    _boolFieldName = [name copy];
+    _boolFieldType = @(fieldType);
+    _boolFieldId = @(fieldID);
+    return YES;
+  }
+  else {
+    return [self writeFieldBeginInternalWithName:name
+                                            type:fieldType
+                                         fieldID:fieldID
+                                    typeOverride:0xFF
+                                           error:error];
   }
 }
 
-- (void) writeFieldBeginInternalWithName: (NSString *) name
-                                    type: (int) fieldType
-                                 fieldID: (int) fieldID
-                            typeOverride: (uint8_t) typeOverride
+-(BOOL) writeFieldBeginInternalWithName:(NSString *)name
+                                   type:(SInt32)fieldType
+                                fieldID:(SInt32)fieldID
+                           typeOverride:(UInt8)typeOverride
+                                  error:(NSError *__autoreleasing *)error
 {
-  uint8_t typeToWrite = typeOverride == 0xFF ? [self compactTypeForTType: fieldType] : typeOverride;
-  
+  UInt8 typeToWrite = typeOverride == 0xFF ? [self compactTypeForTType:fieldType] : typeOverride;
+
   // check if we can use delta encoding for the field id
-  if (fieldID > lastFieldId && fieldID - lastFieldId <= 15) {
+  if (fieldID > _lastFieldId && fieldID - _lastFieldId <= 15) {
     // Write them together
-    [self writeByteDirect: (fieldID - lastFieldId) << 4 | typeToWrite];
-  } else {
-    // Write them separate
-    [self writeByteDirect: typeToWrite];
-    [self writeI16: fieldID];
+    if (![self writeByteDirect:(fieldID - _lastFieldId) << 4 | typeToWrite error:error]) {
+      return NO;
+    }
   }
-  
-  lastFieldId = fieldID;
+  else {
+    // Write them separate
+    if (![self writeByteDirect:typeToWrite error:error]) {
+      return NO;
+    }
+    if (![self writeI16:fieldID error:error]) {
+      return NO;
+    }
+  }
+
+  _lastFieldId = fieldID;
+
+  return YES;
 }
 
-- (void) writeFieldStop
+-(BOOL) writeFieldStop:(NSError *__autoreleasing *)error
 {
-  [self writeByteDirect: TCType_STOP];
+  return [self writeByteDirect:TCType_STOP error:error];
 }
 
-- (void) writeMapBeginWithKeyType: (int) keyType
-                        valueType: (int) valueType
-                             size: (int) size
+-(BOOL) writeMapBeginWithKeyType:(SInt32)keyType
+                       valueType:(SInt32)valueType
+                            size:(SInt32)size
+                           error:(NSError *__autoreleasing *)error
 {
   if (size == 0) {
-    [self writeByteDirect: 0];
-  } else {
-    [self writeVarint32: (uint32_t)size];
-    [self writeByteDirect: [self compactTypeForTType: keyType] << 4 | [self compactTypeForTType: valueType]];
+    if (![self writeByteDirect:0 error:error]) {
+      return NO;
+    }
   }
+  else {
+    if (![self writeVarint32:(UInt32)size error:error]) {
+      return NO;
+    }
+    if (![self writeByteDirect:[self compactTypeForTType:keyType] << 4 | [self compactTypeForTType:valueType] error:error]) {
+      return NO;
+    }
+  }
+  return YES;
 }
 
-- (void) writeListBeginWithElementType: (int) elementType
-                                  size: (int) size
+-(BOOL) writeListBeginWithElementType:(SInt32)elementType
+                                 size:(SInt32)size
+                                error:(NSError *__autoreleasing *)error
 {
-  [self writeCollectionBeginWithElementType: elementType size: size];
+  return [self writeCollectionBeginWithElementType:elementType size:size error:error];
 }
 
-- (void) writeSetBeginWithElementType: (int) elementType
-                                 size: (int) size
+-(BOOL) writeSetBeginWithElementType:(SInt32)elementType
+                                size:(SInt32)size
+                               error:(NSError *__autoreleasing *)error
 {
-  [self writeCollectionBeginWithElementType: elementType size: size];
+  return [self writeCollectionBeginWithElementType:elementType size:size error:error];
 }
 
-- (void) writeBool: (BOOL) b
+-(BOOL) writeBool:(BOOL)b error:(NSError *__autoreleasing *)error
 {
-  if (boolFieldId != nil && boolFieldName != nil && boolFieldType != nil) {
+  BOOL result;
+  if (_boolFieldId != nil && _boolFieldName != nil && _boolFieldType != nil) {
     // we haven't written the field header yet
-    [self writeFieldBeginInternalWithName: boolFieldName
-                                     type: [boolFieldType intValue]
-                                  fieldID: [boolFieldId intValue]
-                             typeOverride: b ? TCType_BOOLEAN_TRUE : TCType_BOOLEAN_FALSE];
-    
-    [boolFieldId release_stub];
-    [boolFieldName release_stub];
-    [boolFieldType release_stub];
-    
-    boolFieldId = nil;
-    boolFieldName = nil;
-    boolFieldType = nil;
-  } else {
+    result = [self writeFieldBeginInternalWithName:_boolFieldName
+                                              type:_boolFieldType.intValue
+                                           fieldID:_boolFieldId.intValue
+                                      typeOverride:b ? TCType_BOOLEAN_TRUE : TCType_BOOLEAN_FALSE
+                                             error:error];
+    _boolFieldId = nil;
+    _boolFieldName = nil;
+    _boolFieldType = nil;
+  }
+  else {
     // we're not part of a field, so just Write the value.
-    [self writeByteDirect: b ? TCType_BOOLEAN_TRUE : TCType_BOOLEAN_FALSE];
+    result = [self writeByteDirect:b ? TCType_BOOLEAN_TRUE : TCType_BOOLEAN_FALSE error:error];
   }
+  return result;
 }
 
-- (void) writeByte: (uint8_t) value
+-(BOOL) writeByte:(UInt8)value error:(NSError *__autoreleasing *)error
 {
-  [self writeByteDirect: value];
+  return [self writeByteDirect:value error:error];
 }
 
-- (void) writeI16: (int16_t) value
+-(BOOL) writeI16:(SInt16)value error:(NSError *__autoreleasing *)error
 {
-  [self writeVarint32: [self i32ToZigZag: value]];
+  return [self writeVarint32:[self i32ToZigZag:value] error:error];
 }
 
-- (void) writeI32: (int32_t) value
+-(BOOL) writeI32:(SInt32)value error:(NSError *__autoreleasing *)error
 {
-  [self writeVarint32: [self i32ToZigZag: value]];
+  return [self writeVarint32:[self i32ToZigZag:value] error:error];
 }
 
-- (void) writeI64: (int64_t) value
+-(BOOL) writeI64:(SInt64)value error:(NSError *__autoreleasing *)error
 {
-  [self writeVarint64: [self i64ToZigZag: value]];
+  return [self writeVarint64:[self i64ToZigZag:value] error:error];
 }
 
-- (void) writeDouble: (double) value
+-(BOOL) writeDouble:(double)value error:(NSError *__autoreleasing *)error
 {
-  //Safe bit-casting double->uint64
-  
-  uint64_t bits = 0;
+  // Safe bit-casting double->uint64
+
+  UInt64 bits = 0;
   memcpy(&bits, &value, 8);
-  
+
   bits = OSSwapHostToLittleInt64(bits);
-  
-  [mTransport write: (uint8_t *)&bits offset: 0 length: 8];
-}
 
-- (void) writeString: (NSString *) value
-{
-  [self writeBinary: [value dataUsingEncoding: NSUTF8StringEncoding]];
-}
-
-- (void) writeBinary: (NSData *) data
-{
-  [self writeVarint32: (uint32_t)data.length];
-  [mTransport write: data.bytes offset: 0 length: data.length];
-}
-
-- (void) writeMessageEnd {}
-- (void) writeMapEnd {}
-- (void) writeListEnd {}
-- (void) writeSetEnd {}
-- (void) writeFieldEnd {}
-
-- (void) writeCollectionBeginWithElementType: (int) elementType
-                                        size: (int) size
-{
-  if (size <= 14) {
-    [self writeByteDirect: size << 4 | [self compactTypeForTType: elementType]];
-  } else {
-    [self writeByteDirect: 0xf0 | [self compactTypeForTType: elementType]];
-    [self writeVarint32: (uint32_t)size];
+  if (![_transport write:(UInt8 *)&bits offset:0 length:8 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
   }
+
+  return YES;
 }
 
-- (void) writeVarint64: (uint64_t) n
+-(BOOL) writeString:(NSString *)value error:(NSError *__autoreleasing *)error
 {
-  uint8_t varint64out[10] = {0};
+  return [self writeBinary:[value dataUsingEncoding:NSUTF8StringEncoding] error:error];
+}
+
+-(BOOL) writeBinary:(NSData *)data error:(NSError *__autoreleasing *)error
+{
+  if (![self writeVarint32:(UInt32)data.length error:error]) {
+    return NO;
+  }
+  if (![_transport write:data.bytes offset:0 length:(UInt32)data.length error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
+  }
+  return YES;
+}
+
+-(BOOL) writeMessageEnd:(NSError *__autoreleasing *)error
+{
+  _currentMessageName = nil;
+  return YES;
+}
+
+-(BOOL) writeMapEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+
+-(BOOL) writeListEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+
+-(BOOL) writeSetEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+
+-(BOOL) writeFieldEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+
+-(BOOL) writeCollectionBeginWithElementType:(SInt32)elementType
+                                       size:(SInt32)size
+                                      error:(NSError *__autoreleasing *)error
+{
+  UInt8 ctypeElement = [self compactTypeForTType:elementType];
+
+  if (size <= 14) {
+    if (![self writeByteDirect:size << 4 | ctypeElement error:error]) {
+      return NO;
+    }
+  }
+  else {
+    if (![self writeByteDirect:0xf0 | ctypeElement error:error]) {
+      return NO;
+    }
+    if (![self writeVarint32:(UInt32)size error:error]) {
+      return NO;
+    }
+  }
+  return YES;
+}
+
+-(BOOL) writeVarint64:(UInt64)n error:(NSError *__autoreleasing *)error
+{
+  UInt8 varint64out[10] = {0};
   int idx = 0;
-  
+
   while (true) {
     if ((n & ~0x7FL) == 0) {
-      varint64out[idx++] = (uint8_t)n;
+      varint64out[idx++] = (UInt8)n;
       break;
-    } else {
-      varint64out[idx++] = (uint8_t)((n & 0x7F) | 0x80);
+    }
+    else {
+      varint64out[idx++] = (UInt8)((n & 0x7F) | 0x80);
       n >>= 7;
     }
   }
-  
-  [mTransport write: varint64out offset: 0 length: idx];
+
+  if (![_transport write:varint64out offset:0 length:idx error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
+  }
+
+  return YES;
 }
 
-- (uint32_t) i32ToZigZag: (int32_t) n
+-(UInt32) i32ToZigZag:(SInt32)n
 {
   /*
-   ZigZag encoding maps signed integers to unsigned integers so that
-   numbers with a small absolute value (for instance, -1) have
-   a small varint encoded value too. It does this in a way that
-   "zig-zags" back and forth through the positive and negative integers,
-   so that -1 is encoded as 1, 1 is encoded as 2, -2 is encoded as 3, and so on
+     ZigZag encoding maps signed integers to unsigned integers so that
+     numbers with a small absolute value (for instance, -1) have
+     a small varint encoded value too. It does this in a way that
+     "zig-zags" back and forth through the positive and negative integers,
+     so that -1 is encoded as 1, 1 is encoded as 2, -2 is encoded as 3, and so
+         on
    */
-  return (uint32_t)(n << 1) ^ (uint32_t)(n >> 31);
+  return (UInt32)(n << 1) ^ (UInt32)(n >> 31);
 }
 
-- (uint64_t) i64ToZigZag: (int64_t) n
+-(UInt64) i64ToZigZag:(SInt64)n
 {
-  return (uint64_t)(n << 1) ^ (uint64_t)(n >> 63);
+  return (UInt64)(n << 1) ^ (UInt64)(n >> 63);
 }
 
-- (void) readMessageBeginReturningName: (NSString **) pname
-                                  type: (int *) ptype
-                            sequenceID: (int *) psequenceID
+-(BOOL) readMessageBeginReturningName:(NSString **)pname
+                                 type:(SInt32 *)ptype
+                           sequenceID:(SInt32 *)psequenceID
+                                error:(NSError *__autoreleasing *)error
 {
-  uint8_t protocolId = [self readByte];
+  UInt8 protocolId;
+  if (![self readByte:&protocolId error:error]) {
+    return NO;
+  }
+
   if (protocolId != COMPACT_PROTOCOL_ID) {
-    @throw [TProtocolException exceptionWithName: @"TProtocolException"
-                                          reason: [NSString stringWithFormat: @"Expected protocol id %X but got %X", COMPACT_PROTOCOL_ID, protocolId]];
+    if (error) {
+      *error = [NSError errorWithDomain:TProtocolErrorDomain
+                                   code:TProtocolErrorUnknown
+                               userInfo:@{TProtocolErrorExtendedErrorKey: @(TProtocolExtendedErrorMismatchedProtocol),
+                                          TProtocolErrorExpectedIdKey: @(COMPACT_PROTOCOL_ID)}];
+    }
+    return NO;
   }
-  
-  uint8_t versionAndType = [self readByte];
-  uint8_t version = versionAndType & COMPACT_VERSION_MASK;
+
+  UInt8 versionAndType;
+  if (![self readByte:&versionAndType error:error]) {
+    return NO;
+  }
+
+  UInt8 version = versionAndType & COMPACT_VERSION_MASK;
   if (version != COMPACT_VERSION) {
-    @throw [TProtocolException exceptionWithName: @"TProtocolException"
-                                          reason: [NSString stringWithFormat: @"Expected version %d but got %d", COMPACT_VERSION, version]];
+    if (error) {
+      *error = [NSError errorWithDomain:TProtocolErrorDomain
+                                   code:TProtocolErrorBadVersion
+                               userInfo:@{TProtocolErrorExpectedVersionKey: @(COMPACT_VERSION)}];
+    }
+    return NO;
   }
-  
+
   int type = (versionAndType >> COMPACT_TYPE_SHIFT_AMOUNT) & COMPACT_TYPE_BITS;
-  int sequenceID = (int)[self readVarint32];
-  NSString* name = [self readString];
-  
+  UInt32 sequenceID;
+  if (![self readVarint32:&sequenceID error:error]) {
+    return NO;
+  }
+  NSString *name;
+  if (![self readString:&name error:error]) {
+    return NO;
+  }
+
   if (ptype != NULL) {
     *ptype = type;
   }
@@ -371,59 +490,74 @@
   if (pname != NULL) {
     *pname = name;
   }
+  return YES;
 }
 
-- (void) readStructBeginReturningName: (NSString **) pname
+-(BOOL) readStructBeginReturningName:(NSString **)pname error:(NSError *__autoreleasing *)error
 {
-  [lastField addObject: [NSNumber numberWithShort: lastFieldId]];
-  lastFieldId = 0;
-  
+  [_lastField addObject:@(_lastFieldId)];
+  _lastFieldId = 0;
+
   if (pname != NULL) {
     *pname = @"";
   }
+
+  return YES;
 }
 
-- (void) readStructEnd
+-(BOOL) readStructEnd:(NSError *__autoreleasing *)error
 {
-  lastFieldId = [[lastField lastObject] shortValue];
-  [lastField removeLastObject];
+  _lastFieldId = [_lastField.lastObject shortValue];
+  [_lastField removeLastObject];
+  return YES;
 }
 
-- (void) readFieldBeginReturningName: (NSString **) pname
-                                type: (int *) pfieldType
-                             fieldID: (int *) pfieldID
+-(BOOL) readFieldBeginReturningName:(NSString **)pname
+                               type:(SInt32 *)pfieldType
+                            fieldID:(SInt32 *)pfieldID
+                              error:(NSError *__autoreleasing *)error
 {
-  uint8_t byte = [self readByte];
-  uint8_t type = byte & 0x0f;
-  
+  UInt8 byte;
+  if (![self readByte:&byte error:error]) {
+    return NO;
+  }
+
+  UInt8 type = byte & 0x0f;
+
   // if it's a stop, then we can return immediately, as the struct is over.
   if (type == TCType_STOP) {
     if (pname != NULL) {
       *pname = @"";
     }
     if (pfieldType != NULL) {
-      *pfieldType = TType_STOP;
+      *pfieldType = TTypeSTOP;
     }
     if (pfieldID != NULL) {
       *pfieldID = 0;
     }
-    return;
+    return YES;
   }
-  
+
   short fieldId = 0;
-  
+
   // mask off the 4 MSB of the type header. it could contain a field id delta.
   short modifier = (byte & 0xf0) >> 4;
   if (modifier == 0) {
     // not a delta. look ahead for the zigzag varint field id.
-    fieldId = [self readI16];
-  } else {
-    // has a delta. add the delta to the last Read field id.
-    fieldId = lastFieldId + modifier;
+    if (![self readI16:&fieldId error:error]) {
+      return NO;
+    }
   }
-  
-  int fieldType = [self ttypeForCompactType: type];
-  
+  else {
+    // has a delta. add the delta to the last Read field id.
+    fieldId = _lastFieldId + modifier;
+  }
+
+  UInt8 fieldType;
+  if (![self ttype:&fieldType forCompactType:type error:error]) {
+    return NO;
+  }
+
   if (pname != NULL) {
     *pname = @"";
   }
@@ -433,31 +567,47 @@
   if (pfieldID != NULL) {
     *pfieldID = fieldId;
   }
-  
+
   // if this happens to be a boolean field, the value is encoded in the type
   if (type == TCType_BOOLEAN_TRUE ||
-      type == TCType_BOOLEAN_FALSE) {
+      type == TCType_BOOLEAN_FALSE)
+  {
     // save the boolean value in a special instance variable.
-    booleanValue = [[NSNumber numberWithBool: type == TCType_BOOLEAN_TRUE] retain_stub];
+    _booleanValue = [NSNumber numberWithBool:type == TCType_BOOLEAN_TRUE];
   }
-  
+
   // push the new field onto the field stack so we can keep the deltas going.
-  lastFieldId = fieldId;
+  _lastFieldId = fieldId;
+
+  return YES;
 }
 
-- (void) readMapBeginReturningKeyType: (int *) pkeyType
-                            valueType: (int *) pvalueType
-                                 size: (int *) psize
+-(BOOL) readMapBeginReturningKeyType:(SInt32 *)pkeyType
+                           valueType:(SInt32 *)pvalueType
+                                size:(SInt32 *)psize
+                               error:(NSError *__autoreleasing *)error
 {
-  uint8_t keyAndValueType = 0;
-  int size = (int)[self readVarint32];
-  if (size != 0) {
-    keyAndValueType = [self readByte];
+  UInt8 keyAndValueType = 0;
+  UInt32 size;
+  if (![self readVarint32:&size error:error]) {
+    return NO;
   }
-  
-  int keyType = [self ttypeForCompactType: keyAndValueType >> 4];
-  int valueType = [self ttypeForCompactType: keyAndValueType & 0xf];
-  
+  if (size != 0) {
+    if (![self readByte:&keyAndValueType error:error]) {
+      return NO;
+    }
+  }
+
+  UInt8 keyType;
+  if (![self ttype:&keyType forCompactType:keyAndValueType >> 4 error:error]) {
+    return NO;
+  }
+
+  UInt8 valueType;
+  if (![self ttype:&valueType forCompactType:keyAndValueType & 0xf error:error]) {
+    return NO;
+  }
+
   if (pkeyType != NULL) {
     *pkeyType = keyType;
   }
@@ -467,220 +617,366 @@
   if (psize != NULL) {
     *psize = size;
   }
+
+  return YES;
 }
 
-- (void) readListBeginReturningElementType: (int *) pelementType
-                                      size: (int *) psize
+-(BOOL) readListBeginReturningElementType:(SInt32 *)pelementType
+                                     size:(SInt32 *)psize
+                                    error:(NSError *__autoreleasing *)error
 {
-  uint8_t size_and_type = [self readByte];
-  int size = (size_and_type >> 4) & 0x0f;
-  if (size == 15) {
-    size = (int)[self readVarint32];
+  UInt8 sizeAndType;
+  if (![self readByte:&sizeAndType error:error]) {
+    return NO;
   }
-  
-  int elementType = [self ttypeForCompactType: size_and_type & 0x0f];
-  
+
+  UInt32 size = (sizeAndType >> 4) & 0x0f;
+  if (size == 15) {
+    if (![self readVarint32:&size error:error]) {
+      return NO;
+    }
+  }
+
+  UInt8 elementType;
+  if (![self ttype:&elementType forCompactType:sizeAndType & 0x0f error:error]) {
+    return NO;
+  }
+
   if (pelementType != NULL) {
     *pelementType = elementType;
   }
   if (psize != NULL) {
     *psize = size;
   }
+
+  return YES;
 }
 
-- (void) readSetBeginReturningElementType: (int *) pelementType
-                                     size: (int *) psize
+-(BOOL) readSetBeginReturningElementType:(SInt32 *)pelementType
+                                    size:(SInt32 *)psize
+                                   error:(NSError *__autoreleasing *)error
 {
-  [self readListBeginReturningElementType: pelementType size: psize];
+  return [self readListBeginReturningElementType:pelementType size:psize error:error];
 }
 
-- (BOOL) readBool
+-(BOOL) readBool:(BOOL *)value error:(NSError *__autoreleasing *)error
 {
-  if (booleanValue != nil) {
-    BOOL result = [booleanValue boolValue];
-    [booleanValue release_stub];
-    booleanValue = nil;
-    return result;
-  } else {
-    return [self readByte] == TCType_BOOLEAN_TRUE;
+  if (_booleanValue != nil) {
+
+    BOOL result = _booleanValue.boolValue;
+    _booleanValue = nil;
+
+    *value = result;
   }
+  else {
+
+    UInt8 result;
+    if (![self readByte:&result error:error]) {
+      return NO;
+    }
+
+    *value = result == TCType_BOOLEAN_TRUE;
+  }
+
+  return YES;
 }
 
-- (uint8_t) readByte
+-(BOOL) readByte:(UInt8 *)value error:(NSError *__autoreleasing *)error
 {
-  uint8_t buf = 0;
-  [mTransport readAll: &buf offset: 0 length: 1];
-  return buf;
+  if (![_transport readAll:value offset:0 length:1 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport read failed");
+  }
+  return YES;
 }
 
-- (int16_t) readI16
+-(BOOL) readI16:(SInt16 *)value error:(NSError *__autoreleasing *)error
 {
-  return (int16_t)[self zigZagToi32: [self readVarint32]];
+  UInt32 v;
+  if (![self readVarint32:&v error:error]) {
+    return NO;
+  }
+
+  if (value) {
+    *value = (SInt16)[self zigZagToi32:v];
+  }
+
+  return YES;
 }
 
-- (int32_t) readI32
+-(BOOL) readI32:(SInt32 *)value error:(NSError *__autoreleasing *)error
 {
-  return [self zigZagToi32: [self readVarint32]];
+  UInt32 v;
+  if (![self readVarint32:&v error:error]) {
+    return NO;
+  }
+
+  if (value) {
+    *value = [self zigZagToi32:v];
+  }
+
+  return YES;
 }
 
-- (int64_t) readI64
+-(BOOL) readI64:(SInt64 *)value error:(NSError *__autoreleasing *)error
 {
-  return [self zigZagToi64: [self readVarint64]];
+  UInt64 v;
+  if (![self readVarint64:&v error:error]) {
+    return NO;
+  }
+
+  if (value) {
+    *value = [self zigZagToi64:v];
+  }
+
+  return YES;
 }
 
-- (double) readDouble
+-(BOOL) readDouble:(double *)value error:(NSError *__autoreleasing *)error
 {
-  uint64_t bits = 0;
-  [mTransport readAll: (uint8_t *)&bits offset: 0 length: 8];
+  UInt64 bits;
+  if (![_transport readAll:(UInt8 *)&bits offset:0 length:8 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport read failed");
+  }
+
   bits = OSSwapLittleToHostInt64(bits);
-  
-  double result = 0;
-  memcpy(&result, &bits, 8);
-  
-  return result;
-}
 
-- (NSString *) readString
-{
-  int length = (int)[self readVarint32];
-  if (length == 0) {
-    return @"";
+  if (value) {
+    memcpy(value, &bits, sizeof(bits));
   }
-  
-  return [[[NSString alloc] initWithData: [self readBinary: length]
-                                encoding: NSUTF8StringEncoding] autorelease_stub];
+
+  return YES;
 }
 
-- (NSData *) readBinary
+-(BOOL) readString:(NSString *__autoreleasing *)value error:(NSError *__autoreleasing *)error
 {
-  return [self readBinary: (int)[self readVarint32]];
-}
-
-- (NSData *) readBinary: (int) length
-{
-  if (length == 0) {
-    return [NSData data];
+  UInt32 length;
+  if (![self readVarint32:&length error:error]) {
+    return NO;
   }
-  
-  NSMutableData* buf = [NSMutableData dataWithLength: length];
-  [mTransport readAll: buf.mutableBytes offset: 0 length: length];
-  return buf;
+
+  NSString *result;
+
+  if (length != 0) {
+
+    NSData *data;
+    if (![self readBinaryOfLength:length data:&data error:error]) {
+      return NO;
+    }
+
+    result = [[NSString alloc] initWithData:data
+                                   encoding:NSUTF8StringEncoding];
+  }
+  else {
+    result = @"";
+  }
+
+  if (value) {
+    *value = result;
+  }
+
+  return YES;
 }
 
-- (void) readMessageEnd {}
-- (void) readFieldEnd {}
-- (void) readMapEnd {}
-- (void) readListEnd {}
-- (void) readSetEnd {}
-
-- (uint32_t) readVarint32
+-(BOOL) readBinary:(NSData *__autoreleasing *)value error:(NSError *__autoreleasing *)error
 {
-  uint32_t result = 0;
+  UInt32 length;
+  if (![self readVarint32:&length error:error]) {
+    return NO;
+  }
+
+  return [self readBinaryOfLength:length data:value error:error];
+}
+
+-(BOOL) readBinaryOfLength:(UInt32)length data:(NSData *__autoreleasing *)value error:(NSError *__autoreleasing *)error
+{
+  NSData *result;
+
+  if (length != 0) {
+
+    NSMutableData *buf = [NSMutableData dataWithLength:length];
+    if (![_transport readAll:buf.mutableBytes offset:0 length:length error:error]) {
+      PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport read failed");
+    }
+
+    result = buf;
+  }
+  else {
+
+    result = [NSData data];
+
+  }
+
+  if (value) {
+    *value = result;
+  }
+
+  return YES;
+}
+
+-(BOOL) readMessageEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+-(BOOL) readFieldEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+-(BOOL) readMapEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+-(BOOL) readListEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+-(BOOL) readSetEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+
+-(BOOL) readVarint32:(UInt32 *)value error:(NSError *__autoreleasing *)error
+{
+  UInt32 result = 0;
   int shift = 0;
-  
+
   while (true) {
-    uint8_t byte = [self readByte];
-    result |= (uint32_t)(byte & 0x7f) << shift;
+
+    UInt8 byte;
+    if (![self readByte:&byte error:error]) {
+      return NO;
+    }
+
+    result |= (UInt32)(byte & 0x7f) << shift;
     if (!(byte & 0x80)) {
       break;
     }
-    
+
     shift += 7;
   }
-  return result;
+
+  if (value) {
+    *value = result;
+  }
+
+  return YES;
 }
 
-- (uint64_t) readVarint64
+-(BOOL) readVarint64:(UInt64 *)value error:(NSError *__autoreleasing *)error
 {
   int shift = 0;
-  uint64_t result = 0;
-  
+  UInt64 result = 0;
+
   while (true) {
-    uint8_t byte = [self readByte];
-    result |= (uint64_t)(byte & 0x7f) << shift;
+
+    UInt8 byte;
+    if (![self readByte:&byte error:error]) {
+      return NO;
+    }
+
+    result |= (UInt64)(byte & 0x7f) << shift;
     if (!(byte & 0x80)) {
       break;
     }
-    
+
     shift += 7;
   }
-  
-  return result;
+
+  if (value) {
+    *value = result;
+  }
+
+  return YES;
 }
 
-- (int32_t) zigZagToi32: (uint32_t) n
+-(SInt32) zigZagToi32:(UInt32)n
 {
-  return (int32_t)(n >> 1) ^ (-(int32_t)(n & 1));
+  return (SInt32)(n >> 1) ^ (-(SInt32)(n & 1));
 }
 
-- (int64_t) zigZagToi64: (uint64_t) n
+-(SInt64) zigZagToi64:(UInt64)n
 {
-  return (int64_t)(n >> 1) ^ (-(int64_t)(n & 1));
+  return (SInt64)(n >> 1) ^ (-(SInt64)(n & 1));
 }
 
-- (uint8_t) ttypeForCompactType: (uint8_t) type
+-(BOOL) ttype:(UInt8 *)ttype forCompactType:(UInt8)ctype error:(NSError *__autoreleasing *)error
 {
-  switch (type & 0x0f) {
-    case TCType_STOP:
-      return TType_STOP;
-      
-    case TCType_BOOLEAN_FALSE:
-    case TCType_BOOLEAN_TRUE:
-      return TType_BOOL;
-      
-    case TCType_BYTE:
-      return TType_BYTE;
-      
-    case TCType_I16:
-      return TType_I16;
-      
-    case TCType_I32:
-      return TType_I32;
-      
-    case TCType_I64:
-      return TType_I64;
-      
-    case TCType_DOUBLE:
-      return TType_DOUBLE;
-      
-    case TCType_BINARY:
-      return TType_STRING;
-      
-    case TCType_LIST:
-      return TType_LIST;
-      
-    case TCType_SET:
-      return TType_SET;
-      
-    case TCType_MAP:
-      return TType_MAP;
-      
-    case TCType_STRUCT:
-      return TType_STRUCT;
-      
-    default:
-      @throw [TProtocolException exceptionWithName: @"TProtocolException"
-                                            reason: [NSString stringWithFormat: @"Don't know what type: %d", (uint8_t)(type & 0x0F)]];
+  switch (ctype & 0x0f) {
+  case TCType_STOP:
+    *ttype = TTypeSTOP;
+    return YES;
+
+  case TCType_BOOLEAN_FALSE:
+  case TCType_BOOLEAN_TRUE:
+    *ttype = TTypeBOOL;
+    return YES;
+
+  case TCType_BYTE:
+    *ttype = TTypeBYTE;
+    return YES;
+
+  case TCType_I16:
+    *ttype = TTypeI16;
+    return YES;
+
+  case TCType_I32:
+    *ttype = TTypeI32;
+    return YES;
+
+  case TCType_I64:
+    *ttype = TTypeI64;
+    return YES;
+
+  case TCType_DOUBLE:
+    *ttype = TTypeDOUBLE;
+    return YES;
+
+  case TCType_BINARY:
+    *ttype = TTypeSTRING;
+    return YES;
+
+  case TCType_LIST:
+    *ttype = TTypeLIST;
+    return YES;
+
+  case TCType_SET:
+    *ttype = TTypeSET;
+    return YES;
+
+  case TCType_MAP:
+    *ttype = TTypeMAP;
+    return YES;
+
+  case TCType_STRUCT:
+    *ttype = TTypeSTRUCT;
+    return YES;
+
+  default:
+    if (error) {
+      *error = [NSError errorWithDomain:TProtocolErrorDomain
+                                   code:TProtocolErrorUnknown
+                               userInfo:@{TProtocolErrorTypeKey: @((UInt8)(ctype & 0x0F))}];
+    }
+    return NO;
   }
 }
 
-- (uint8_t) compactTypeForTType: (uint8_t) ttype
+-(UInt8) compactTypeForTType:(UInt8)ttype
 {
-  static uint8_t ttypeToCompactType[] = {
-    [TType_STOP] = TCType_STOP,
-    [TType_BOOL] = TCType_BOOLEAN_FALSE,
-    [TType_BYTE] = TCType_BYTE,
-    [TType_DOUBLE] = TCType_DOUBLE,
-    [TType_I16] = TCType_I16,
-    [TType_I32] = TCType_I32,
-    [TType_I64] = TCType_I64,
-    [TType_STRING] = TCType_BINARY,
-    [TType_STRUCT] = TCType_STRUCT,
-    [TType_MAP] = TCType_MAP,
-    [TType_SET] = TCType_SET,
-    [TType_LIST] = TCType_LIST
+  static UInt8 ttypeToCompactType[] = {
+    [TTypeSTOP] = TCType_STOP,
+    [TTypeBOOL] = TCType_BOOLEAN_FALSE,
+    [TTypeBYTE] = TCType_BYTE,
+    [TTypeDOUBLE] = TCType_DOUBLE,
+    [TTypeI16] = TCType_I16,
+    [TTypeI32] = TCType_I32,
+    [TTypeI64] = TCType_I64,
+    [TTypeSTRING] = TCType_BINARY,
+    [TTypeSTRUCT] = TCType_STRUCT,
+    [TTypeMAP] = TCType_MAP,
+    [TTypeSET] = TCType_SET,
+    [TTypeLIST] = TCType_LIST
   };
-  
+
   return ttypeToCompactType[ttype];
 }
 
diff --git a/lib/cocoa/src/protocol/TMultiplexedProtocol.h b/lib/cocoa/src/protocol/TMultiplexedProtocol.h
index f298459..b8ce361 100644
--- a/lib/cocoa/src/protocol/TMultiplexedProtocol.h
+++ b/lib/cocoa/src/protocol/TMultiplexedProtocol.h
@@ -21,13 +21,18 @@
 
 #import "TProtocolDecorator.h"
 
-FOUNDATION_EXPORT NSString *const MULTIPLEXED_SERVICE_SEPERATOR;
+NS_ASSUME_NONNULL_BEGIN
 
-@interface TMultiplexedProtocol : TProtocolDecorator {
-    NSString * mServiceName;
-}
 
-- (id) initWithProtocol: (id <TProtocol>) protocol
-            serviceName: (NSString *) name;
+extern NSString *TMultiplexedProtocolSeperator;
+
+
+@interface TMultiplexedProtocol : TProtocolDecorator
+
+-(id) initWithProtocol:(id <TProtocol>)protocol
+           serviceName:(NSString *)name;
 
 @end
+
+
+NS_ASSUME_NONNULL_END
\ No newline at end of file
diff --git a/lib/cocoa/src/protocol/TMultiplexedProtocol.m b/lib/cocoa/src/protocol/TMultiplexedProtocol.m
index 49095e3..5838c57 100644
--- a/lib/cocoa/src/protocol/TMultiplexedProtocol.m
+++ b/lib/cocoa/src/protocol/TMultiplexedProtocol.m
@@ -20,48 +20,47 @@
 #import "TMultiplexedProtocol.h"
 
 #import "TProtocol.h"
-#import "TObjective-C.h"
 
-NSString *const MULTIPLEXED_SERVICE_SEPERATOR = @":";
+NSString *TMultiplexedProtocolSeperator = @":";
+
+
+@interface TMultiplexedProtocol ()
+
+@property(strong, nonatomic) NSString *serviceName;
+
+@end
+
 
 @implementation TMultiplexedProtocol
 
-- (id) initWithProtocol: (id <TProtocol>) protocol
-            serviceName: (NSString *) name
+-(id) initWithProtocol:(id <TProtocol>)protocol
+           serviceName:(NSString *)name
 {
-    self = [super initWithProtocol:protocol];
-
-    if (self) {
-        mServiceName = [name retain_stub];
-    }
-    return self;
+  self = [super initWithProtocol:protocol];
+  if (self) {
+    _serviceName = name;
+  }
+  return self;
 }
 
-- (void) writeMessageBeginWithName: (NSString *) name
-                              type: (int) messageType
-                        sequenceID: (int) sequenceID
+-(BOOL) writeMessageBeginWithName:(NSString *)name
+                             type:(SInt32)messageType
+                       sequenceID:(SInt32)sequenceID
+                            error:(NSError *__autoreleasing *)error
 {
-    switch (messageType) {
-        case TMessageType_CALL:
-        case TMessageType_ONEWAY:
-            {
-                NSMutableString * serviceFunction = [[NSMutableString alloc] initWithString:mServiceName];
-                [serviceFunction appendString:MULTIPLEXED_SERVICE_SEPERATOR];
-                [serviceFunction appendString:name];
-                [super writeMessageBeginWithName:serviceFunction type:messageType sequenceID:sequenceID];
-                [serviceFunction release_stub];
-            }
-            break;
-        default:
-            [super writeMessageBeginWithName:name type:messageType sequenceID:sequenceID];
-            break;
-    }
-}
+  switch (messageType) {
+  case TMessageTypeCALL:
+  case TMessageTypeONEWAY: {
+    NSMutableString *serviceFunction = [[NSMutableString alloc] initWithString:_serviceName];
+    [serviceFunction appendString:TMultiplexedProtocolSeperator];
+    [serviceFunction appendString:name];
+    return [super writeMessageBeginWithName:serviceFunction type:messageType sequenceID:sequenceID error:error];
+  }
+  break;
 
-- (void) dealloc
-{
-    [mServiceName release_stub];
-    [super dealloc_stub];
+  default:
+    return [super writeMessageBeginWithName:name type:messageType sequenceID:sequenceID error:error];
+  }
 }
 
 @end
diff --git a/lib/cocoa/src/protocol/TProtocol.h b/lib/cocoa/src/protocol/TProtocol.h
index 281239d..841059f 100644
--- a/lib/cocoa/src/protocol/TProtocol.h
+++ b/lib/cocoa/src/protocol/TProtocol.h
@@ -22,127 +22,143 @@
 #import "TTransport.h"
 
 
-enum {
-  TMessageType_CALL = 1,
-  TMessageType_REPLY = 2,
-  TMessageType_EXCEPTION = 3,
-  TMessageType_ONEWAY = 4
+NS_ASSUME_NONNULL_BEGIN
+
+
+typedef NS_ENUM (int, TMessageType) {
+  TMessageTypeCALL = 1,
+  TMessageTypeREPLY = 2,
+  TMessageTypeEXCEPTION = 3,
+  TMessageTypeONEWAY = 4
 };
 
-enum {
-  TType_STOP   = 0,
-  TType_VOID   = 1,
-  TType_BOOL   = 2,
-  TType_BYTE   = 3,
-  TType_DOUBLE = 4,
-  TType_I16    = 6,
-  TType_I32    = 8,
-  TType_I64    = 10,
-  TType_STRING = 11,
-  TType_STRUCT = 12,
-  TType_MAP    = 13,
-  TType_SET    = 14,
-  TType_LIST   = 15
+typedef NS_ENUM (int, TType) {
+  TTypeSTOP   = 0,
+  TTypeVOID   = 1,
+  TTypeBOOL   = 2,
+  TTypeBYTE   = 3,
+  TTypeDOUBLE = 4,
+  TTypeI16    = 6,
+  TTypeI32    = 8,
+  TTypeI64    = 10,
+  TTypeSTRING = 11,
+  TTypeSTRUCT = 12,
+  TTypeMAP    = 13,
+  TTypeSET    = 14,
+  TTypeLIST   = 15
 };
 
 
 @protocol TProtocol <NSObject>
 
-- (id <TTransport>) transport;
+-(id <TTransport>) transport;
 
-- (void) readMessageBeginReturningName: (NSString **) name
-                                  type: (int *) type
-                            sequenceID: (int *) sequenceID;
-- (void) readMessageEnd;
+-(BOOL) readMessageBeginReturningName:(NSString *__nullable __autoreleasing *__nullable)name
+                                 type:(nullable SInt32 *)type
+                           sequenceID:(nullable SInt32 *)sequenceID
+                                error:(NSError *__autoreleasing *)error;
+-(BOOL) readMessageEnd:(NSError *__autoreleasing *)error;
 
-- (void) readStructBeginReturningName: (NSString **) name;
-- (void) readStructEnd;
+-(BOOL) readStructBeginReturningName:(NSString *__nullable __autoreleasing *__nullable)name
+                               error:(NSError *__autoreleasing *)error;
+-(BOOL) readStructEnd:(NSError *__autoreleasing *)error;
 
-- (void) readFieldBeginReturningName: (NSString **) name
-                                type: (int *) fieldType
-                             fieldID: (int *) fieldID;
-- (void) readFieldEnd;
+-(BOOL) readFieldBeginReturningName:(NSString *__nullable __autoreleasing *__nullable)name
+                               type:(SInt32 *)fieldType
+                            fieldID:(nullable SInt32 *)fieldID
+                              error:(NSError *__autoreleasing *)error;
+-(BOOL) readFieldEnd:(NSError *__autoreleasing *)error;
 
-- (NSString *) readString;
+-(BOOL) readString:(NSString *__nonnull __autoreleasing *__nonnull)value error:(NSError **)error;
 
-- (BOOL) readBool;
+-(BOOL) readBool:(BOOL *)value error:(NSError *__autoreleasing *)error;
 
-- (unsigned char) readByte;
+-(BOOL) readByte:(UInt8 *)value error:(NSError *__autoreleasing *)error;
 
-- (short) readI16;
+-(BOOL) readI16:(SInt16 *)value error:(NSError *__autoreleasing *)error;
 
-- (int32_t) readI32;
+-(BOOL) readI32:(SInt32 *)value error:(NSError *__autoreleasing *)error;
 
-- (int64_t) readI64;
+-(BOOL) readI64:(SInt64 *)value error:(NSError *__autoreleasing *)error;
 
-- (double) readDouble;
+-(BOOL) readDouble:(double *)value error:(NSError *__autoreleasing *)error;
 
-- (NSData *) readBinary;
+-(BOOL) readBinary:(NSData *__nonnull __autoreleasing *__nonnull)value error:(NSError **)error;
 
-- (void) readMapBeginReturningKeyType: (int *) keyType
-                            valueType: (int *) valueType
-                                 size: (int *) size;
-- (void) readMapEnd;
+-(BOOL) readMapBeginReturningKeyType:(nullable SInt32 *)keyType
+                           valueType:(nullable SInt32 *)valueType
+                                size:(SInt32 *)size
+                               error:(NSError *__autoreleasing *)error;
+-(BOOL) readMapEnd:(NSError *__autoreleasing *)error;
 
 
-- (void) readSetBeginReturningElementType: (int *) elementType
-                                     size: (int *) size;
-- (void) readSetEnd;
+-(BOOL) readSetBeginReturningElementType:(nullable SInt32 *)elementType
+                                    size:(SInt32 *)size
+                                   error:(NSError *__autoreleasing *)error;
+-(BOOL) readSetEnd:(NSError *__autoreleasing *)error;
 
 
-- (void) readListBeginReturningElementType: (int *) elementType
-                                      size: (int *) size;
-- (void) readListEnd;
+-(BOOL) readListBeginReturningElementType:(nullable SInt32 *)elementType
+                                     size:(SInt32 *)size
+                                    error:(NSError *__autoreleasing *)error;
+-(BOOL) readListEnd:(NSError *__autoreleasing *)error;
 
 
-- (void) writeMessageBeginWithName: (NSString *) name
-                              type: (int) messageType
-                        sequenceID: (int) sequenceID;
-- (void) writeMessageEnd;
+-(BOOL) writeMessageBeginWithName:(NSString *)name
+                             type:(SInt32)messageType
+                       sequenceID:(SInt32)sequenceID
+                            error:(NSError *__autoreleasing *)error;
+-(BOOL) writeMessageEnd:(NSError *__autoreleasing *)error;
 
-- (void) writeStructBeginWithName: (NSString *) name;
-- (void) writeStructEnd;
+-(BOOL) writeStructBeginWithName:(NSString *)name error:(NSError **)error;
+-(BOOL) writeStructEnd:(NSError *__autoreleasing *)error;
 
-- (void) writeFieldBeginWithName: (NSString *) name
-                            type: (int) fieldType
-                         fieldID: (int) fieldID;
+-(BOOL) writeFieldBeginWithName:(NSString *)name
+                           type:(SInt32)fieldType
+                        fieldID:(SInt32)fieldID
+                          error:(NSError *__autoreleasing *)error;
 
-- (void) writeI32: (int32_t) value;
+-(BOOL) writeI32:(SInt32)value error:(NSError *__autoreleasing *)error;
 
-- (void) writeI64: (int64_t) value;
+-(BOOL) writeI64:(SInt64)value error:(NSError *__autoreleasing *)error;
 
-- (void) writeI16: (short) value;
+-(BOOL) writeI16:(short)value error:(NSError *__autoreleasing *)error;
 
-- (void) writeByte: (uint8_t) value;
+-(BOOL) writeByte:(UInt8)value error:(NSError *__autoreleasing *)error;
 
-- (void) writeString: (NSString *) value;
+-(BOOL) writeString:(NSString *)value error:(NSError *__autoreleasing *)error;
 
-- (void) writeDouble: (double) value;
+-(BOOL) writeDouble:(double)value error:(NSError *__autoreleasing *)error;
 
-- (void) writeBool: (BOOL) value;
+-(BOOL) writeBool:(BOOL)value error:(NSError *__autoreleasing *)error;
 
-- (void) writeBinary: (NSData *) data;
+-(BOOL) writeBinary:(NSData *)data error:(NSError *__autoreleasing *)error;
 
-- (void) writeFieldStop;
+-(BOOL) writeFieldStop:(NSError *__autoreleasing *)error;
 
-- (void) writeFieldEnd;
+-(BOOL) writeFieldEnd:(NSError *__autoreleasing *)error;
 
-- (void) writeMapBeginWithKeyType: (int) keyType
-                        valueType: (int) valueType
-                             size: (int) size;
-- (void) writeMapEnd;
+-(BOOL) writeMapBeginWithKeyType:(SInt32)keyType
+                       valueType:(SInt32)valueType
+                            size:(SInt32)size
+                           error:(NSError *__autoreleasing *)error;
+-(BOOL) writeMapEnd:(NSError *__autoreleasing *)error;
 
 
-- (void) writeSetBeginWithElementType: (int) elementType
-                                 size: (int) size;
-- (void) writeSetEnd;
+-(BOOL) writeSetBeginWithElementType:(SInt32)elementType
+                                size:(SInt32)size
+                               error:(NSError *__autoreleasing *)error;
+-(BOOL) writeSetEnd:(NSError *__autoreleasing *)error;
 
 
-- (void) writeListBeginWithElementType: (int) elementType
-                                  size: (int) size;
+-(BOOL) writeListBeginWithElementType:(SInt32)elementType
+                                 size:(SInt32)size
+                                error:(NSError *__autoreleasing *)error;
 
-- (void) writeListEnd;
+-(BOOL) writeListEnd:(NSError *__autoreleasing *)error;
 
 
 @end
 
+
+NS_ASSUME_NONNULL_END
diff --git a/lib/cocoa/src/protocol/TProtocolDecorator.h b/lib/cocoa/src/protocol/TProtocolDecorator.h
index 829bed6..369b6a2 100644
--- a/lib/cocoa/src/protocol/TProtocolDecorator.h
+++ b/lib/cocoa/src/protocol/TProtocolDecorator.h
@@ -21,10 +21,14 @@
 
 #import "TProtocol.h"
 
-@interface TProtocolDecorator : NSObject <TProtocol> {
-    id<TProtocol> mConcreteProtocol;
-}
+NS_ASSUME_NONNULL_BEGIN
 
-- (id) initWithProtocol: (id <TProtocol>) protocol;
+
+@interface TProtocolDecorator : NSObject <TProtocol>
+
+-(id) initWithProtocol:(id <TProtocol>)protocol;
 
 @end
+
+
+NS_ASSUME_NONNULL_END
\ No newline at end of file
diff --git a/lib/cocoa/src/protocol/TProtocolDecorator.m b/lib/cocoa/src/protocol/TProtocolDecorator.m
index e5acb6c..218f900 100644
--- a/lib/cocoa/src/protocol/TProtocolDecorator.m
+++ b/lib/cocoa/src/protocol/TProtocolDecorator.m
@@ -18,257 +18,278 @@
  */
 
 #import "TProtocolDecorator.h"
-#import "TObjective-C.h"
+
+
+@interface TProtocolDecorator ()
+
+@property(strong, nonatomic) id<TProtocol> concreteProtocol;
+
+@end
+
 
 @implementation TProtocolDecorator
 
-- (id) initWithProtocol: (id <TProtocol>) protocol
+-(id) initWithProtocol:(id <TProtocol>)protocol
 {
-    self = [super init];
-    if (self) {
-        mConcreteProtocol = [protocol retain_stub];
-    }
-    return self;
+  self = [super init];
+  if (self) {
+    _concreteProtocol = protocol;
+  }
+  return self;
 }
 
-- (id <TTransport>) transport
+-(id <TTransport>) transport
 {
-    return [mConcreteProtocol transport];
+  return [_concreteProtocol transport];
 }
 
-- (void) readMessageBeginReturningName: (NSString **) name
-                                  type: (int *) type
-                            sequenceID: (int *) sequenceID
+-(BOOL) readMessageBeginReturningName:(NSString **)name
+                                 type:(SInt32 *)type
+                           sequenceID:(SInt32 *)sequenceID
+                                error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readMessageBeginReturningName:name
-                                                type:type
-                                          sequenceID:sequenceID];
+  return [_concreteProtocol readMessageBeginReturningName:name
+                                                     type:type
+                                               sequenceID:sequenceID
+                                                    error:error];
 }
 
-- (void) readMessageEnd
+-(BOOL) readMessageEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readMessageEnd];
+  return [_concreteProtocol readMessageEnd:error];
 }
 
-- (void) readStructBeginReturningName: (NSString **) name
+-(BOOL) readStructBeginReturningName:(NSString **)name
+                               error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readStructBeginReturningName:name];
+  return [_concreteProtocol readStructBeginReturningName:name error:error];
 }
 
-- (void) readStructEnd
+-(BOOL) readStructEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readStructEnd];
+  return [_concreteProtocol readStructEnd:error];
 }
 
-- (void) readFieldBeginReturningName: (NSString **) name
-                                type: (int *) fieldType
-                             fieldID: (int *) fieldID
+-(BOOL) readFieldBeginReturningName:(NSString **)name
+                               type:(SInt32 *)fieldType
+                            fieldID:(SInt32 *)fieldID
+                              error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readFieldBeginReturningName:name
-                                              type:fieldType
-                                           fieldID:fieldID];
+  return [_concreteProtocol readFieldBeginReturningName:name
+                                                   type:fieldType
+                                                fieldID:fieldID
+                                                  error:error];
 }
-- (void) readFieldEnd
+-(BOOL) readFieldEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readFieldEnd];
+  return [_concreteProtocol readFieldEnd:error];
 }
 
-- (NSString *) readString
+-(BOOL) readString:(NSString *__autoreleasing *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readString];
+  return [_concreteProtocol readString:value error:error];
 }
 
-- (BOOL) readBool
+-(BOOL) readBool:(BOOL *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readBool];
+  return [_concreteProtocol readBool:value error:error];
 }
 
-- (unsigned char) readByte
+-(BOOL) readByte:(UInt8 *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readByte];
+  return [_concreteProtocol readByte:value error:error];
 }
 
-- (short) readI16
+-(BOOL) readI16:(SInt16 *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readI16];
+  return [_concreteProtocol readI16:value error:error];
 }
 
-- (int32_t) readI32
+-(BOOL) readI32:(SInt32 *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readI32];
+  return [_concreteProtocol readI32:value error:error];
 }
 
-- (int64_t) readI64
+-(BOOL) readI64:(SInt64 *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readI64];
+  return [_concreteProtocol readI64:value error:error];
 }
 
-- (double) readDouble
+-(BOOL) readDouble:(double *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readDouble];
+  return [_concreteProtocol readDouble:value error:error];
 }
 
-- (NSData *) readBinary
+-(BOOL) readBinary:(NSData *__autoreleasing *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readBinary];
+  return [_concreteProtocol readBinary:value error:error];
 }
 
-- (void) readMapBeginReturningKeyType: (int *) keyType
-                            valueType: (int *) valueType
-                                 size: (int *) size
+-(BOOL) readMapBeginReturningKeyType:(SInt32 *)keyType
+                           valueType:(SInt32 *)valueType
+                                size:(SInt32 *)size
+                               error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readMapBeginReturningKeyType:keyType
-                                          valueType:valueType
-                                               size:size];
+  return [_concreteProtocol readMapBeginReturningKeyType:keyType
+                                               valueType:valueType
+                                                    size:size
+                                                   error:error];
 }
-- (void) readMapEnd
+-(BOOL) readMapEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readMapEnd];
+  return [_concreteProtocol readMapEnd:error];
 }
 
 
-- (void) readSetBeginReturningElementType: (int *) elementType
-                                     size: (int *) size
+-(BOOL) readSetBeginReturningElementType:(SInt32 *)elementType
+                                    size:(SInt32 *)size
+                                   error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readSetBeginReturningElementType:elementType
-                                                   size:size];
+  return [_concreteProtocol readSetBeginReturningElementType:elementType
+                                                        size:size
+                                                       error:error];
 }
-- (void) readSetEnd
+-(BOOL) readSetEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readSetEnd];
+  return [_concreteProtocol readSetEnd:error];
 }
 
-- (void) readListBeginReturningElementType: (int *) elementType
-                                      size: (int *) size
+-(BOOL) readListBeginReturningElementType:(SInt32 *)elementType
+                                     size:(SInt32 *)size
+                                    error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readListBeginReturningElementType:elementType
-                                                    size:size];
+  return [_concreteProtocol readListBeginReturningElementType:elementType
+                                                         size:size
+                                                        error:error];
 }
-- (void) readListEnd
+-(BOOL) readListEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readListEnd];
+  return [_concreteProtocol readListEnd:error];
 }
 
-- (void) writeMessageBeginWithName: (NSString *) name
-                              type: (int) messageType
-                        sequenceID: (int) sequenceID
+-(BOOL) writeMessageBeginWithName:(NSString *)name
+                             type:(SInt32)messageType
+                       sequenceID:(SInt32)sequenceID
+                            error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeMessageBeginWithName:name
-                                            type:messageType
-                                      sequenceID:sequenceID];
+  return [_concreteProtocol writeMessageBeginWithName:name
+                                                 type:messageType
+                                           sequenceID:sequenceID
+                                                error:error];
 }
-- (void) writeMessageEnd
+-(BOOL) writeMessageEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeMessageEnd];
+  return [_concreteProtocol writeMessageEnd:error];
 }
 
-- (void) writeStructBeginWithName: (NSString *) name
+-(BOOL) writeStructBeginWithName:(NSString *)name error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeStructBeginWithName:name];
+  return [_concreteProtocol writeStructBeginWithName:name error:error];
 }
-- (void) writeStructEnd
+-(BOOL) writeStructEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeStructEnd];
+  return [_concreteProtocol writeStructEnd:error];
 }
 
-- (void) writeFieldBeginWithName: (NSString *) name
-                            type: (int) fieldType
-                         fieldID: (int) fieldID
+-(BOOL) writeFieldBeginWithName:(NSString *)name
+                           type:(SInt32)fieldType
+                        fieldID:(SInt32)fieldID
+                          error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeFieldBeginWithName:name
-                                          type:fieldType
-                                       fieldID:fieldID];
+  return [_concreteProtocol writeFieldBeginWithName:name
+                                               type:fieldType
+                                            fieldID:fieldID
+                                              error:error];
 }
 
-- (void) writeI32: (int32_t) value
+-(BOOL) writeI32:(SInt32)value error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeI32:value];
+  return [_concreteProtocol writeI32:value error:error];
 }
 
-- (void) writeI64: (int64_t) value
+-(BOOL) writeI64:(SInt64)value error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeI64:value];
+  return [_concreteProtocol writeI64:value error:error];
 }
 
-- (void) writeI16: (short) value
+-(BOOL) writeI16:(SInt16)value error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeI16:value];
+  return [_concreteProtocol writeI16:value error:error];
 }
 
-- (void) writeByte: (uint8_t) value
+-(BOOL) writeByte:(UInt8)value error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeByte:value];
+  return [_concreteProtocol writeByte:value error:error];
 }
 
-- (void) writeString: (NSString *) value
+-(BOOL) writeString:(NSString *)value error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeString:value];
+  return [_concreteProtocol writeString:value error:error];
 }
 
-- (void) writeDouble: (double) value
+-(BOOL) writeDouble:(double)value error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeDouble:value];
+  return [_concreteProtocol writeDouble:value error:error];
 }
 
-- (void) writeBool: (BOOL) value
+-(BOOL) writeBool:(BOOL)value error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeBool:value];
+  return [_concreteProtocol writeBool:value error:error];
 }
 
-- (void) writeBinary: (NSData *) data
+-(BOOL) writeBinary:(NSData *)data error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeBinary:data];
+  return [_concreteProtocol writeBinary:data error:error];
 }
 
-- (void) writeFieldStop
+-(BOOL) writeFieldStop:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeFieldStop];
+  return [_concreteProtocol writeFieldStop:error];
 }
 
-- (void) writeFieldEnd
+-(BOOL) writeFieldEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeFieldEnd];
+  return [_concreteProtocol writeFieldEnd:error];
 }
 
-- (void) writeMapBeginWithKeyType: (int) keyType
-                        valueType: (int) valueType
-                             size: (int) size
+-(BOOL) writeMapBeginWithKeyType:(SInt32)keyType
+                       valueType:(SInt32)valueType
+                            size:(SInt32)size
+                           error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeMapBeginWithKeyType:keyType
-                                      valueType:valueType
-                                           size:size];
-}
-- (void) writeMapEnd
-{
-    [mConcreteProtocol writeMapEnd];
+  return [_concreteProtocol writeMapBeginWithKeyType:keyType
+                                           valueType:valueType
+                                                size:size
+                                               error:error];
 }
 
-- (void) writeSetBeginWithElementType: (int) elementType
-                                 size: (int) size
+-(BOOL) writeMapEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeSetBeginWithElementType:elementType size:size];
+  return [_concreteProtocol writeMapEnd:error];
 }
 
-- (void) writeSetEnd
+-(BOOL) writeSetBeginWithElementType:(SInt32)elementType
+                                size:(SInt32)size
+                               error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeSetEnd];
+  return [_concreteProtocol writeSetBeginWithElementType:elementType size:size error:error];
 }
 
-- (void) writeListBeginWithElementType: (int) elementType
-                                  size: (int) size
+-(BOOL) writeSetEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeListBeginWithElementType:elementType size:size];
+  return [_concreteProtocol writeSetEnd:error];
 }
 
-- (void) writeListEnd
+-(BOOL) writeListBeginWithElementType:(SInt32)elementType
+                                 size:(SInt32)size
+                                error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeListEnd];
+  return [_concreteProtocol writeListBeginWithElementType:elementType size:size error:error];
 }
 
-- (void) dealloc
+-(BOOL) writeListEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol release_stub];
-    [super dealloc_stub];
+  return [_concreteProtocol writeListEnd:error];
 }
 
 @end
diff --git a/lib/cocoa/src/protocol/TProtocolError.h b/lib/cocoa/src/protocol/TProtocolError.h
new file mode 100644
index 0000000..ab0bc40
--- /dev/null
+++ b/lib/cocoa/src/protocol/TProtocolError.h
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#import "TError.h"
+
+
+extern NSString *TProtocolErrorDomain;
+
+typedef NS_ENUM (int, TProtocolError) {
+  TProtocolErrorUnknown                   = 0,
+  TProtocolErrorInvalidData               = 1,
+  TProtocolErrorNegativeSize              = 2,
+  TProtocolErrorSizeLimit                 = 3,
+  TProtocolErrorBadVersion                = 4,
+  TProtocolErrorNotImplemented            = 5,
+  TProtocolErrorDepthLimit                = 6,
+};
+
+
+typedef NS_ENUM(int, TProtocolExtendedError) {
+  TProtocolExtendedErrorMissingRequiredField  = 1001,
+  TProtocolExtendedErrorUnexpectedType        = 1002,
+  TProtocolExtendedErrorMismatchedProtocol    = 1003,
+};
+
+extern NSString *TProtocolErrorExtendedErrorKey;
+extern NSString *TProtocolErrorFieldNameKey;
+extern NSString *TProtocolErrorExpectedIdKey;
+extern NSString *TProtocolErrorExpectedVersionKey;
+extern NSString *TProtocolErrorTypeKey;
+extern NSString *TProtocolErrorSourceLineKey;
+extern NSString *TProtocolErrorSourceFileKey;
+extern NSString *TProtocolErrorSourceMethodKey;
+extern NSString *TProtocolErrorMessageNameKey;
+
+
+#define PROTOCOL_ERROR(ret, err, ...) \
+  if (error) {  \
+    *error = [NSError errorWithDomain:TProtocolErrorDomain \
+                                 code:TProtocolError ## err \
+                             userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:__VA_ARGS__], \
+                                        @"SourceFile": [NSString stringWithUTF8String:__FILE__], \
+                                        @"SourceLine": @(__LINE__), \
+                                        @"SourceFunction": [NSString stringWithUTF8String:__PRETTY_FUNCTION__], \
+                                        @"Message": self.currentMessageName ? self.currentMessageName : @""}]; \
+  } \
+  return ret
+
+#define PROTOCOL_TRANSPORT_ERROR(ret, errorPtr, ...) \
+  if (errorPtr) { \
+    *error = [NSError errorWithDomain:TProtocolErrorDomain \
+                                 code:TProtocolErrorUnknown \
+                             userInfo:@{NSLocalizedDescriptionKey: [[NSString stringWithFormat:__VA_ARGS__] stringByAppendingFormat:@": %@", [(*errorPtr) localizedDescription]], \
+                                        TProtocolErrorSourceFileKey: [NSString stringWithUTF8String:__FILE__], \
+                                        TProtocolErrorSourceLineKey: @(__LINE__), \
+                                        TProtocolErrorSourceMethodKey: [NSString stringWithUTF8String:__PRETTY_FUNCTION__], \
+                                        TProtocolErrorMessageNameKey: self.currentMessageName ? self.currentMessageName : @"", \
+                                        NSUnderlyingErrorKey: *errorPtr}]; \
+  } \
+  return ret
diff --git a/lib/cocoa/src/protocol/TProtocolError.m b/lib/cocoa/src/protocol/TProtocolError.m
new file mode 100644
index 0000000..953673b
--- /dev/null
+++ b/lib/cocoa/src/protocol/TProtocolError.m
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#import "TProtocolError.h"
+
+
+NSString *TProtocolErrorDomain = @"TProtocolErrorDomain";
+
+NSString *TProtocolErrorExtendedErrorKey = @"extendedError";
+NSString *TProtocolErrorFieldNameKey = @"field";
+NSString *TProtocolErrorExpectedIdKey = @"expectedId";
+NSString *TProtocolErrorExpectedVersionKey = @"expectedVersion";
+NSString *TProtocolErrorTypeKey = @"type";
+NSString *TProtocolErrorSourceLineKey = @"sourceLine";
+NSString *TProtocolErrorSourceFileKey = @"sourceFile";
+NSString *TProtocolErrorSourceMethodKey = @"sourceMethod";
+NSString *TProtocolErrorMessageNameKey = @"messageName";
diff --git a/lib/cocoa/src/protocol/TProtocolFactory.h b/lib/cocoa/src/protocol/TProtocolFactory.h
index f200a6d..a022a7f 100644
--- a/lib/cocoa/src/protocol/TProtocolFactory.h
+++ b/lib/cocoa/src/protocol/TProtocolFactory.h
@@ -21,9 +21,16 @@
 #import "TProtocol.h"
 #import "TTransport.h"
 
+NS_ASSUME_NONNULL_BEGIN
+
 
 @protocol TProtocolFactory <NSObject>
 
-- (id <TProtocol>) newProtocolOnTransport: (id <TTransport>) transport;
+@property (readonly, nonatomic) NSString *protocolName;
+
+-(id<TProtocol>) newProtocolOnTransport:(id<TTransport>)transport;
 
 @end
+
+
+NS_ASSUME_NONNULL_END
diff --git a/lib/cocoa/src/protocol/TProtocolUtil.h b/lib/cocoa/src/protocol/TProtocolUtil.h
index 757748a..82510cf 100644
--- a/lib/cocoa/src/protocol/TProtocolUtil.h
+++ b/lib/cocoa/src/protocol/TProtocolUtil.h
@@ -20,10 +20,14 @@
 #import "TProtocol.h"
 #import "TTransport.h"
 
-@interface TProtocolUtil : NSObject {
+NS_ASSUME_NONNULL_BEGIN
 
-}
 
-+ (void) skipType: (int) type onProtocol: (id <TProtocol>) protocol;
+@interface TProtocolUtil : NSObject
 
-@end
++(BOOL) skipType:(int)type onProtocol:(id <TProtocol>)protocol error:(NSError **)error;
+
+@end;
+
+
+NS_ASSUME_NONNULL_END
\ No newline at end of file
diff --git a/lib/cocoa/src/protocol/TProtocolUtil.m b/lib/cocoa/src/protocol/TProtocolUtil.m
index 13d7095..c0d65ac 100644
--- a/lib/cocoa/src/protocol/TProtocolUtil.m
+++ b/lib/cocoa/src/protocol/TProtocolUtil.m
@@ -21,84 +21,151 @@
 
 @implementation TProtocolUtil
 
-+ (void) skipType: (int) type onProtocol: (id <TProtocol>) protocol
++(BOOL) skipType:(int)type onProtocol:(id <TProtocol>)protocol error:(NSError **)error
 {
   switch (type) {
-  case TType_BOOL:
-    [protocol readBool];
-    break;
-  case TType_BYTE:
-    [protocol readByte];
-    break;
-  case TType_I16:
-    [protocol readI16];
-    break;
-  case TType_I32:
-    [protocol readI32];
-    break;
-  case TType_I64:
-    [protocol readI64];
-    break;
-  case TType_DOUBLE:
-    [protocol readDouble];
-    break;
-  case TType_STRING:
-    [protocol readString];
-    break;
-  case TType_STRUCT:
-    [protocol readStructBeginReturningName: NULL];
+  case TTypeBOOL: {
+    BOOL val;
+    if (![protocol readBool:&val error:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeBYTE: {
+    UInt8 val;
+    if (![protocol readByte:&val error:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeI16: {
+    SInt16 val;
+    if (![protocol readI16:&val error:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeI32: {
+    SInt32 val;
+    if (![protocol readI32:&val error:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeI64: {
+    SInt64 val;
+    if (![protocol readI64:&val error:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeDOUBLE: {
+    double val;
+    if (![protocol readDouble:&val error:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeSTRING: {
+    NSString *val;
+    if (![protocol readString:&val error:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeSTRUCT: {
+    if (![protocol readStructBeginReturningName:NULL error:error]) {
+      return NO;
+    }
     while (true) {
-      int fieldType;
-      [protocol readFieldBeginReturningName: nil type: &fieldType fieldID: nil];
-      if (fieldType == TType_STOP) {
+      SInt32 fieldType;
+      if (![protocol readFieldBeginReturningName:nil type:&fieldType fieldID:nil error:error]) {
+        return NO;
+      }
+      if (fieldType == TTypeSTOP) {
         break;
       }
-      [TProtocolUtil skipType: fieldType onProtocol: protocol];
-      [protocol readFieldEnd];
+      if (![self skipType:fieldType onProtocol:protocol error:error]) {
+        return NO;
+      }
+      if (![protocol readFieldEnd:error]) {
+        return NO;
+      }
     }
-    [protocol readStructEnd];
-    break;
-  case TType_MAP:
-  {
-    int keyType;
-    int valueType;
-    int size;
-    [protocol readMapBeginReturningKeyType: &keyType valueType: &valueType size: &size];
+    if (![protocol readStructEnd:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeMAP: {
+    SInt32 keyType;
+    SInt32 valueType;
+    SInt32 size;
+    if (![protocol readMapBeginReturningKeyType:&keyType valueType:&valueType size:&size error:error]) {
+      return NO;
+    }
     int i;
     for (i = 0; i < size; i++) {
-      [TProtocolUtil skipType: keyType onProtocol: protocol];
-      [TProtocolUtil skipType: valueType onProtocol: protocol];
-    }
-    [protocol readMapEnd];
-  }
-    break;
-    case TType_SET:
-    {
-      int elemType;
-      int size;
-      [protocol readSetBeginReturningElementType: &elemType size: &size];
-      int i;
-      for (i = 0; i < size; i++) {
-        [TProtocolUtil skipType: elemType onProtocol: protocol];
+      if (![TProtocolUtil skipType:keyType onProtocol:protocol error:error]) {
+        return NO;
       }
-      [protocol readSetEnd];
-    }
-      break;
-    case TType_LIST:
-    {
-      int elemType;
-      int size;
-      [protocol readListBeginReturningElementType: &elemType size: &size];
-      int i;
-      for (i = 0; i < size; i++) {
-        [TProtocolUtil skipType: elemType onProtocol: protocol];
+      if (![TProtocolUtil skipType:valueType onProtocol:protocol error:error]) {
+        return NO;
       }
-      [protocol readListEnd];
     }
-      break;
-    default:
-      return;
+    if (![protocol readMapEnd:error]) {
+      return NO;
+    }
   }
+  break;
+
+  case TTypeSET: {
+    SInt32 elemType;
+    SInt32 size;
+    if (![protocol readSetBeginReturningElementType:&elemType size:&size error:error]) {
+      return NO;
+    }
+    int i;
+    for (i = 0; i < size; i++) {
+      if (![TProtocolUtil skipType:elemType onProtocol:protocol error:error]) {
+        return NO;
+      }
+    }
+    if (![protocol readSetEnd:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeLIST: {
+    SInt32 elemType;
+    SInt32 size;
+    if (![protocol readListBeginReturningElementType:&elemType size:&size error:error]) {
+      return NO;
+    }
+    int i;
+    for (i = 0; i < size; i++) {
+      if (![TProtocolUtil skipType:elemType onProtocol:protocol error:error]) {
+        return NO;
+      }
+    }
+    if (![protocol readListEnd:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  }
+
+  return YES;
 }
 
 @end
diff --git a/lib/cocoa/src/server/TSocketServer.h b/lib/cocoa/src/server/TSocketServer.h
index c8ff9f0..fe657ea 100644
--- a/lib/cocoa/src/server/TSocketServer.h
+++ b/lib/cocoa/src/server/TSocketServer.h
@@ -27,23 +27,21 @@
 #import <CFNetwork/CFNetwork.h>
 #endif
 
-extern NSString * const kTSocketServer_ClientConnectionFinishedForProcessorNotification;
-extern NSString * const kTSocketServer_ProcessorKey;
-extern NSString * const kTSockerServer_TransportKey;
+NS_ASSUME_NONNULL_BEGIN
 
 
-@interface TSocketServer : NSObject {
-  NSFileHandle * mSocketFileHandle;
-  id <TProtocolFactory> mInputProtocolFactory;
-  id <TProtocolFactory> mOutputProtocolFactory;
-  id <TProcessorFactory> mProcessorFactory;
-}
+extern NSString *const TSocketServerClientConnectionFinished;
+extern NSString *const TSocketServerProcessorKey;
+extern NSString *const TSockerServerTransportKey;
 
-- (id) initWithPort: (int) port
-    protocolFactory: (id <TProtocolFactory>) protocolFactory
-   processorFactory: (id <TProcessorFactory>) processorFactory;
+
+@interface TSocketServer : NSObject
+
+-(instancetype) initWithPort:(int)port
+             protocolFactory:(id <TProtocolFactory>)protocolFactory
+            processorFactory:(id <TProcessorFactory>)processorFactory;
 
 @end
 
 
-
+NS_ASSUME_NONNULL_END
diff --git a/lib/cocoa/src/server/TSocketServer.m b/lib/cocoa/src/server/TSocketServer.m
index 07bc829..ccbbba1 100644
--- a/lib/cocoa/src/server/TSocketServer.m
+++ b/lib/cocoa/src/server/TSocketServer.m
@@ -21,37 +21,51 @@
 #import "TSocketServer.h"
 #import "TNSFileHandleTransport.h"
 #import "TProtocol.h"
-#import "TTransportException.h"
-#import "TObjective-C.h"
+#import "TTransportError.h"
+
 #import <sys/socket.h>
 #include <netinet/in.h>
 
 
 
-NSString * const kTSocketServer_ClientConnectionFinishedForProcessorNotification = @"TSocketServer_ClientConnectionFinishedForProcessorNotification";
-NSString * const kTSocketServer_ProcessorKey = @"TSocketServer_Processor";
-NSString * const kTSockerServer_TransportKey = @"TSockerServer_Transport";
+NSString *const TSocketServerClientConnectionFinished = @"TSocketServerClientConnectionFinished";
+NSString *const TSocketServerProcessorKey = @"TSocketServerProcessor";
+NSString *const TSockerServerTransportKey = @"TSockerServerTransport";
+
+
+@interface TSocketServer ()
+
+@property(strong, nonatomic) id<TProtocolFactory> inputProtocolFactory;
+@property(strong, nonatomic) id<TProtocolFactory> outputProtocolFactory;
+@property(strong, nonatomic) id<TProcessorFactory> processorFactory;
+@property(strong, nonatomic) NSFileHandle *socketFileHandle;
+@property(strong, nonatomic) dispatch_queue_t processingQueue;
+
+@end
 
 
 @implementation TSocketServer
 
-- (id) initWithPort: (int) port
-    protocolFactory: (id <TProtocolFactory>) protocolFactory
-   processorFactory: (id <TProcessorFactory>) processorFactory
+-(instancetype) initWithPort:(int)port
+             protocolFactory:(id <TProtocolFactory>)protocolFactory
+            processorFactory:(id <TProcessorFactory>)processorFactory;
 {
   self = [super init];
 
-  mInputProtocolFactory = [protocolFactory retain_stub];
-  mOutputProtocolFactory = [protocolFactory retain_stub];
-  mProcessorFactory = [processorFactory retain_stub];
+  _inputProtocolFactory = protocolFactory;
+  _outputProtocolFactory = protocolFactory;
+  _processorFactory = processorFactory;
+
+  dispatch_queue_attr_t processingQueueAttr =
+    dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_CONCURRENT, QOS_CLASS_BACKGROUND, 0);
+
+  _processingQueue = dispatch_queue_create("TSocketServer.processing", processingQueueAttr);
 
   // create a socket.
   int fd = -1;
   CFSocketRef socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL);
   if (socket) {
-    CFOptionFlags flagsToClear = kCFSocketCloseOnInvalidate;
-    CFSocketSetSocketFlags(socket,  CFSocketGetSocketFlags(socket) & ~flagsToClear);
-
+    CFSocketSetSocketFlags(socket, CFSocketGetSocketFlags(socket) & ~kCFSocketCloseOnInvalidate);
     fd = CFSocketGetNative(socket);
     int yes = 1;
     setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes));
@@ -63,135 +77,88 @@
     addr.sin_port = htons(port);
     addr.sin_addr.s_addr = htonl(INADDR_ANY);
     NSData *address = [NSData dataWithBytes:&addr length:sizeof(addr)];
-    if (CFSocketSetAddress(socket, (bridge_stub CFDataRef)address) != kCFSocketSuccess) {
+    if (CFSocketSetAddress(socket, (__bridge CFDataRef)address) != kCFSocketSuccess) {
       CFSocketInvalidate(socket);
       CFRelease(socket);
-      NSLog(@"*** Could not bind to address");
+      NSLog(@"TSocketServer: Could not bind to address");
       return nil;
     }
-  } else {
-    NSLog(@"*** No server socket");
+  }
+  else {
+    NSLog(@"TSocketServer: No server socket");
     return nil;
   }
-  
+
   // wrap it in a file handle so we can get messages from it
-  mSocketFileHandle = [[NSFileHandle alloc] initWithFileDescriptor: fd
-                                                    closeOnDealloc: YES];
-  
+  _socketFileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fd
+                                                    closeOnDealloc:YES];
+
   // throw away our socket
   CFSocketInvalidate(socket);
   CFRelease(socket);
-  
-    // register for notifications of accepted incoming connections
-  [[NSNotificationCenter defaultCenter] addObserver: self
-                                           selector: @selector(connectionAccepted:)
-                                               name: NSFileHandleConnectionAcceptedNotification
-                                             object: mSocketFileHandle];
-  
+
+  // register for notifications of accepted incoming connections
+  [[NSNotificationCenter defaultCenter] addObserver:self
+                                           selector:@selector(connectionAccepted:)
+                                               name:NSFileHandleConnectionAcceptedNotification
+                                             object:_socketFileHandle];
+
   // tell socket to listen
-  [mSocketFileHandle acceptConnectionInBackgroundAndNotify];
-  
-  NSLog(@"Listening on TCP port %d", port);
-  
+  [_socketFileHandle acceptConnectionInBackgroundAndNotify];
+
+  NSLog(@"TSocketServer: Listening on TCP port %d", port);
+
   return self;
 }
 
 
-- (void) dealloc {
+-(void) dealloc
+{
   [[NSNotificationCenter defaultCenter] removeObserver:self];
-  [mInputProtocolFactory release_stub];
-  [mOutputProtocolFactory release_stub];
-  [mProcessorFactory release_stub];
-  [mSocketFileHandle release_stub];
-  [super dealloc_stub];
 }
 
 
-- (void) connectionAccepted: (NSNotification *) aNotification
+-(void) connectionAccepted:(NSNotification *)notification
 {
-  NSFileHandle * socket = [[aNotification userInfo] objectForKey: NSFileHandleNotificationFileHandleItem];
+  NSFileHandle *socket = [notification.userInfo objectForKey:NSFileHandleNotificationFileHandleItem];
 
-  // now that we have a client connected, spin off a thread to handle activity
-  [NSThread detachNewThreadSelector: @selector(handleClientConnection:)
-                           toTarget: self
-                         withObject: socket];
+  // Now that we have a client connected, handle request on queue
+  dispatch_async(_processingQueue, ^{
 
-  [[aNotification object] acceptConnectionInBackgroundAndNotify];
+    [self handleClientConnection:socket];
+
+  });
+
+  // Continue accepting connections
+  [_socketFileHandle acceptConnectionInBackgroundAndNotify];
 }
 
 
-- (void) handleClientConnection: (NSFileHandle *) clientSocket
+-(void) handleClientConnection:(NSFileHandle *)clientSocket
 {
-#if __has_feature(objc_arc)
-    @autoreleasepool {
-        TNSFileHandleTransport * transport = [[TNSFileHandleTransport alloc] initWithFileHandle: clientSocket];
-        id<TProcessor> processor = [mProcessorFactory processorForTransport: transport];
-        
-        id <TProtocol> inProtocol = [mInputProtocolFactory newProtocolOnTransport: transport];
-        id <TProtocol> outProtocol = [mOutputProtocolFactory newProtocolOnTransport: transport];
-        
-        @try {
-            BOOL result = NO;
-            do {
-                @autoreleasepool {
-                    result = [processor processOnInputProtocol: inProtocol outputProtocol: outProtocol];
-                }
-            } while (result);
-        }
-        @catch (TTransportException * te) {
-            (void)te;
-            //NSLog(@"Caught transport exception, abandoning client connection: %@", te);
-        }
-        
-        NSNotification * n = [NSNotification notificationWithName: kTSocketServer_ClientConnectionFinishedForProcessorNotification
-                                                           object: self
-                                                         userInfo: [NSDictionary dictionaryWithObjectsAndKeys: 
-                                                                    processor,
-                                                                    kTSocketServer_ProcessorKey,
-                                                                    transport,
-                                                                    kTSockerServer_TransportKey,
-                                                                    nil]];
-        [[NSNotificationCenter defaultCenter] performSelectorOnMainThread: @selector(postNotification:) withObject: n waitUntilDone: YES];
-        
+  @autoreleasepool {
+
+    TNSFileHandleTransport *transport = [[TNSFileHandleTransport alloc] initWithFileHandle:clientSocket];
+    id<TProcessor> processor = [_processorFactory processorForTransport:transport];
+
+    id <TProtocol> inProtocol = [_inputProtocolFactory newProtocolOnTransport:transport];
+    id <TProtocol> outProtocol = [_outputProtocolFactory newProtocolOnTransport:transport];
+
+    NSError *error;
+    if (![processor processOnInputProtocol:inProtocol outputProtocol:outProtocol error:&error]) {
+      // Handle error
+      NSLog(@"Error processing request: %@", error);
     }
-#else
-  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
-  
-  TNSFileHandleTransport * transport = [[TNSFileHandleTransport alloc] initWithFileHandle: clientSocket];
-  id<TProcessor> processor = [mProcessorFactory processorForTransport: transport];
-  
-  id <TProtocol> inProtocol = [[mInputProtocolFactory newProtocolOnTransport: transport] autorelease];
-  id <TProtocol> outProtocol = [[mOutputProtocolFactory newProtocolOnTransport: transport] autorelease];
 
-  @try {
-    BOOL result = NO;
-    do {
-      NSAutoreleasePool * myPool = [[NSAutoreleasePool alloc] init];
-      result = [processor processOnInputProtocol: inProtocol outputProtocol: outProtocol];
-      [myPool release];
-    } while (result);
-  }
-  @catch (TTransportException * te) {
-    //NSLog(@"Caught transport exception, abandoning client connection: %@", te);
-  }
+    dispatch_async(dispatch_get_main_queue(), ^{
 
-  NSNotification * n = [NSNotification notificationWithName: kTSocketServer_ClientConnectionFinishedForProcessorNotification
-                                                     object: self
-                                                   userInfo: [NSDictionary dictionaryWithObjectsAndKeys: 
-                                                              processor,
-                                                              kTSocketServer_ProcessorKey,
-                                                              transport,
-                                                              kTSockerServer_TransportKey,
-                                                              nil]];
-  [[NSNotificationCenter defaultCenter] performSelectorOnMainThread: @selector(postNotification:) withObject: n waitUntilDone: YES];
-  
-  [pool release];
-#endif
+      [NSNotificationCenter.defaultCenter postNotificationName:TSocketServerClientConnectionFinished
+                                                        object:self
+                                                      userInfo:@{TSocketServerProcessorKey: processor,
+                                                                 TSockerServerTransportKey: transport}];
+    });
+
+  }
 }
 
-
-
 @end
-
-
-
diff --git a/lib/cocoa/src/transport/TAsyncTransport.h b/lib/cocoa/src/transport/TAsyncTransport.h
index f75b701..bab4fbd 100644
--- a/lib/cocoa/src/transport/TAsyncTransport.h
+++ b/lib/cocoa/src/transport/TAsyncTransport.h
@@ -18,12 +18,29 @@
  */
 
 #import "TTransport.h"
-#import "TException.h"
 
-typedef void(^TAsyncFailureBlock)(TException *);
+NS_ASSUME_NONNULL_BEGIN
+
+
+@protocol TAsyncTransport;
+
+
+@protocol TAsyncTransportFactory <NSObject>
+
+-(id<TAsyncTransport>) newTransport;
+
+@end
+
+
+typedef void (^TAsyncCompletionBlock)();
+typedef void (^TAsyncFailureBlock)(NSError * __nonnull);
+
 
 @protocol TAsyncTransport <TTransport>
 
-- (void) flush:(dispatch_block_t)flushed failure:(TAsyncFailureBlock)failure;
+-(void) flushWithCompletion:(TAsyncCompletionBlock)completed failure:(TAsyncFailureBlock)failure;
 
 @end
+
+
+NS_ASSUME_NONNULL_END
\ No newline at end of file
diff --git a/lib/cocoa/src/transport/TFramedTransport.h b/lib/cocoa/src/transport/TFramedTransport.h
index fc38877..ea68ac4 100644
--- a/lib/cocoa/src/transport/TFramedTransport.h
+++ b/lib/cocoa/src/transport/TFramedTransport.h
@@ -20,10 +20,14 @@
 #import <Foundation/Foundation.h>
 #import "TTransport.h"
 
-@interface TFramedTransport : NSObject <TTransport> {
-    id <TTransport> mTransport;
-}
+NS_ASSUME_NONNULL_BEGIN
 
-- (id) initWithTransport: (id <TTransport>) transport;
+
+@interface TFramedTransport : NSObject <TTransport>
+
+-(id) initWithTransport:(id <TTransport>)transport;
 
 @end
+
+
+NS_ASSUME_NONNULL_END
diff --git a/lib/cocoa/src/transport/TFramedTransport.m b/lib/cocoa/src/transport/TFramedTransport.m
index 2148806..4db65c4 100644
--- a/lib/cocoa/src/transport/TFramedTransport.m
+++ b/lib/cocoa/src/transport/TFramedTransport.m
@@ -18,126 +18,163 @@
  */
 
 #import "TFramedTransport.h"
-#import "TTransportException.h"
-#import "TObjective-C.h"
+#import "TTransportError.h"
 
 #define HEADER_SIZE 4
 #define INIT_FRAME_SIZE 1024
 
-@implementation TFramedTransport {
-    NSMutableData* writeBuffer;
-    NSMutableData* readBuffer;
-    NSUInteger readOffset;
-    uint8_t dummy_header[HEADER_SIZE];
+
+@interface TFramedTransport ()
+
+@property(strong, nonatomic) id<TTransport> transport;
+@property(strong, nonatomic) NSMutableData *writeBuffer;
+@property(strong, nonatomic) NSMutableData *readBuffer;
+@property(assign, nonatomic) NSUInteger readOffset;
+
+@end
+
+
+@implementation TFramedTransport
+
+-(id) initWithTransport:(id <TTransport>)aTransport
+{
+  if ((self = [self init])) {
+    _transport = aTransport;
+    _readBuffer = nil;
+    _readOffset = 0;
+    _writeBuffer = [NSMutableData dataWithLength:HEADER_SIZE];
+  }
+  return self;
 }
 
-- (id) initWithTransport:(id <TTransport>)transport
+-(BOOL) flush:(NSError **)error
 {
-    mTransport = [transport retain_stub];
-    readBuffer = nil;
-    readOffset = 0;
-    writeBuffer = [[NSMutableData alloc] initWithCapacity:INIT_FRAME_SIZE];
-    [writeBuffer appendBytes:dummy_header length:HEADER_SIZE];
-    return self;
+  int len = (int)[_writeBuffer length];
+  int data_len = len - HEADER_SIZE;
+  if (data_len < 0) {
+    if (error) {
+      *error = [NSError errorWithDomain:TTransportErrorDomain
+                                   code:TTransportErrorUnknown
+                               userInfo:@{}];
+    }
+    return NO;
+  }
+
+  UInt8 i32rd[HEADER_SIZE];
+  i32rd[0] = (UInt8)(0xff & (data_len >> 24));
+  i32rd[1] = (UInt8)(0xff & (data_len >> 16));
+  i32rd[2] = (UInt8)(0xff & (data_len >> 8));
+  i32rd[3] = (UInt8)(0xff & (data_len));
+
+  // should we make a copy of the writeBuffer instead? Better for threaded
+  //  operations!
+  [_writeBuffer replaceBytesInRange:NSMakeRange(0, HEADER_SIZE)
+                          withBytes:i32rd length:HEADER_SIZE];
+
+  if (![_transport write:_writeBuffer.mutableBytes offset:0 length:len error:error]) {
+    return NO;
+  }
+
+  if (![_transport flush:error]) {
+    return NO;
+  }
+
+  _writeBuffer.length = HEADER_SIZE;
+
+  return YES;
 }
 
-- (void) dealloc
+-(BOOL) write:(const UInt8 *)data offset:(UInt32)offset length:(UInt32)length error:(NSError *__autoreleasing *)error
 {
-    [mTransport release_stub];
-    [writeBuffer release_stub];
-    if (readBuffer != nil)
-        [readBuffer release_stub];
-    [super dealloc_stub];
+  [_writeBuffer appendBytes:data+offset length:length];
+
+  return YES;
 }
 
-- (void)flush
+-(BOOL) readAll:(UInt8 *)outBuffer offset:(UInt32)outBufferOffset length:(UInt32)length error:(NSError *__autoreleasing *)error
 {
-    size_t headerAndDataLength = [writeBuffer length];
-    if (headerAndDataLength < HEADER_SIZE) {
-        @throw [TTransportException exceptionWithReason:@"Framed transport buffer has no header"];
+  UInt32 got = [self readAvail:outBuffer offset:outBufferOffset maxLength:length error:error];
+  if (got != length) {
+
+    // Report underflow only if readAvail didn't report error already
+    if (error && !*error) {
+      *error = [NSError errorWithDomain:TTransportErrorDomain
+                                   code:TTransportErrorEndOfFile
+                               userInfo:nil];
     }
 
-    size_t dataLength = headerAndDataLength - HEADER_SIZE;
-    uint8_t i32rd[HEADER_SIZE];
-    i32rd[0] = (uint8_t)(0xff & (dataLength >> 24));
-    i32rd[1] = (uint8_t)(0xff & (dataLength >> 16));
-    i32rd[2] = (uint8_t)(0xff & (dataLength >> 8));
-    i32rd[3] = (uint8_t)(0xff & (dataLength));
+    return NO;
+  }
 
-    // should we make a copy of the writeBuffer instead? Better for threaded operations!
-    [writeBuffer replaceBytesInRange:NSMakeRange(0, HEADER_SIZE) withBytes:i32rd length:HEADER_SIZE];
-    [mTransport write:[writeBuffer mutableBytes] offset:0 length:headerAndDataLength];
-    [mTransport flush];
-
-    // reuse old memory buffer
-    [writeBuffer setLength:0];
-    [writeBuffer appendBytes:dummy_header length:HEADER_SIZE];
+  return YES;
 }
 
-- (void) write: (const uint8_t *) data offset: (size_t) offset length: (size_t) length
+-(UInt32) readAvail:(UInt8 *)outBuffer offset:(UInt32)outBufferOffset maxLength:(UInt32)length error:(NSError *__autoreleasing *)error
 {
-    [writeBuffer appendBytes:data+offset length:length];
+  UInt32 got = 0;
+  while (got < length) {
+
+    NSUInteger avail = _readBuffer.length - _readOffset;
+    if (avail == 0) {
+      if (![self readFrame:error]) {
+        return 0;
+      }
+      avail = _readBuffer.length;
+    }
+
+    NSRange range;
+    range.location = _readOffset;
+    range.length = MIN(length - got, avail);
+
+    [_readBuffer getBytes:outBuffer+outBufferOffset+got range:range];
+    _readOffset += range.length;
+    got += range.length;
+  }
+
+  return got;
 }
 
-- (size_t) readAll: (uint8_t *) buf offset: (size_t) offset length: (size_t) length
+-(BOOL) readFrame:(NSError **)error
 {
-    if (readBuffer == nil) {
-        [self readFrame];
-    }
-    
-    if (readBuffer != nil) {
-        size_t bufferLength = [readBuffer length];
-        if (bufferLength - readOffset >= length) {
-            [readBuffer getBytes:buf range:NSMakeRange(readOffset,length)]; // copy data
-            readOffset += length;
-        } else {
-            // void the previous readBuffer data and request a new frame
-            [self readFrame];
-            [readBuffer getBytes:buf range:NSMakeRange(0,length)]; // copy data
-            readOffset = length;
-        }
-    }
-    return length;
-}
+  UInt8 i32rd[HEADER_SIZE];
+  if (![_transport readAll:i32rd offset:0 length:HEADER_SIZE error:error]) {
+    return NO;
+  }
 
-- (void)readFrame
-{
-    uint8_t i32rd[HEADER_SIZE];
-    [mTransport readAll: i32rd offset: 0 length: HEADER_SIZE];
-    int32_t headerValue =
-        ((i32rd[0] & 0xff) << 24) |
-        ((i32rd[1] & 0xff) << 16) |
-        ((i32rd[2] & 0xff) <<  8) |
-        ((i32rd[3] & 0xff));
-    if (headerValue < 0) {
-        NSString *reason = [NSString stringWithFormat:
-                            @"Frame header reports negative frame size: %"PRId32,
-                            headerValue];
-        @throw [TTransportException exceptionWithReason:reason];
+  SInt32 size =
+    ((i32rd[0] & 0xff) << 24) |
+    ((i32rd[1] & 0xff) << 16) |
+    ((i32rd[2] & 0xff) <<  8) |
+    ((i32rd[3] & 0xff));
+
+  if (_readBuffer == nil) {
+
+    _readBuffer = [NSMutableData dataWithLength:size];
+
+  }
+  else {
+
+    SInt32 len = (SInt32)_readBuffer.length;
+    if (len >= size) {
+
+      _readBuffer.length = size;
+
+    }
+    else {
+
+      // increase length of data buffer
+      [_readBuffer increaseLengthBy:size-len];
+
     }
 
-    /* Cast should be safe:
-     * Have verified headerValue non-negative and of lesser or equal bitwidth to size_t. */
-    size_t frameSize = (size_t)headerValue;
-    [self ensureReadBufferHasLength:frameSize];
+  }
 
-    [mTransport readAll:[readBuffer mutableBytes] offset:0 length:frameSize];
-}
+  // copy into internal memory buffer
+  if (![_transport readAll:_readBuffer.mutableBytes offset:0 length:size error:error]) {
+    return NO;
+  }
 
-- (void)ensureReadBufferHasLength:(size_t)length
-{
-    if (readBuffer == nil) {
-        readBuffer = [[NSMutableData alloc] initWithLength:length];
-    } else {
-        size_t currentLength = [readBuffer length];
-        BOOL isTooLong = (currentLength >= length);
-        if (isTooLong) {
-            [readBuffer setLength:length];
-        } else {
-            size_t lengthToAdd = length - currentLength;
-            [readBuffer increaseLengthBy:lengthToAdd];
-        }
-    }
+  return YES;
 }
 
 @end
diff --git a/lib/cocoa/src/transport/THTTPClient.h b/lib/cocoa/src/transport/THTTPClient.h
deleted file mode 100644
index 78935fb..0000000
--- a/lib/cocoa/src/transport/THTTPClient.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#import <Foundation/Foundation.h>
-#import "TTransport.h"
-
-@interface THTTPClient : NSObject <TTransport> {
-  NSURL * mURL;
-  NSMutableURLRequest * mRequest;
-  NSMutableData * mRequestData;
-  NSData * mResponseData;
-  size_t mResponseDataOffset;
-  NSString * mUserAgent;
-  int mTimeout;
-}
-
-- (id) initWithURL: (NSURL *) aURL;
-
-- (id) initWithURL: (NSURL *) aURL
-         userAgent: (NSString *) userAgent
-           timeout: (int) timeout;
-
-- (void) setURL: (NSURL *) aURL;
-
-@end
-
diff --git a/lib/cocoa/src/transport/THTTPClient.m b/lib/cocoa/src/transport/THTTPClient.m
deleted file mode 100644
index 169927c..0000000
--- a/lib/cocoa/src/transport/THTTPClient.m
+++ /dev/null
@@ -1,161 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#import "THTTPClient.h"
-#import "TTransportException.h"
-#import "TObjective-C.h"
-
-@implementation THTTPClient
-
-
-- (void) setupRequest
-{
-  if (mRequest != nil) {
-    [mRequest release_stub];
-  }
-
-  // set up our request object that we'll use for each request
-  mRequest = [[NSMutableURLRequest alloc] initWithURL: mURL];
-  [mRequest setHTTPMethod: @"POST"];
-  [mRequest setValue: @"application/x-thrift" forHTTPHeaderField: @"Content-Type"];
-  [mRequest setValue: @"application/x-thrift" forHTTPHeaderField: @"Accept"];
-
-  NSString * userAgent = mUserAgent;
-  if (!userAgent) {
-    userAgent = @"Cocoa/THTTPClient";
-  }
-  [mRequest setValue: userAgent forHTTPHeaderField: @"User-Agent"];
-
-  [mRequest setCachePolicy: NSURLRequestReloadIgnoringCacheData];
-  if (mTimeout) {
-    [mRequest setTimeoutInterval: mTimeout];
-  }
-}
-
-
-- (id) initWithURL: (NSURL *) aURL
-{
-  return [self initWithURL: aURL
-                 userAgent: nil
-                   timeout: 0];
-}
-
-
-- (id) initWithURL: (NSURL *) aURL
-         userAgent: (NSString *) userAgent
-           timeout: (int) timeout
-{
-  self = [super init];
-  if (!self) {
-    return nil;
-  }
-
-  mTimeout = timeout;
-  if (userAgent) {
-    mUserAgent = [userAgent retain_stub];
-  }
-  mURL = [aURL retain_stub];
-
-  [self setupRequest];
-
-  // create our request data buffer
-  mRequestData = [[NSMutableData alloc] initWithCapacity: 1024];
-
-  return self;
-}
-
-
-- (void) setURL: (NSURL *) aURL
-{
-  [aURL retain_stub];
-  [mURL release_stub];
-  mURL = aURL;
-
-  [self setupRequest];
-}
-
-
-- (void) dealloc
-{
-  [mURL release_stub];
-  [mUserAgent release_stub];
-  [mRequest release_stub];
-  [mRequestData release_stub];
-  [mResponseData release_stub];
-  [super dealloc_stub];
-}
-
-
-- (size_t) readAll: (uint8_t *) buf offset: (size_t) offset length: (size_t) length
-{
-  NSRange r;
-  r.location = mResponseDataOffset;
-  r.length = length;
-
-  [mResponseData getBytes: buf+offset range: r];
-  mResponseDataOffset += length;
-
-  return length;
-}
-
-
-- (void) write: (const uint8_t *) data offset: (size_t) offset length: (size_t) length
-{
-  [mRequestData appendBytes: data+offset length: length];
-}
-
-
-- (void) flush
-{
-  [mRequest setHTTPBody: mRequestData]; // not sure if it copies the data
-
-  // make the HTTP request
-  NSURLResponse * response;
-  NSError * error;
-  NSData * responseData =
-    [NSURLConnection sendSynchronousRequest: mRequest returningResponse: &response error: &error];
-
-  [mRequestData setLength: 0];
-
-  if (responseData == nil) {
-    @throw [TTransportException exceptionWithName: @"TTransportException"
-                                reason: @"Could not make HTTP request"
-                                error: error];
-  }
-  if (![response isKindOfClass: [NSHTTPURLResponse class]]) {
-    @throw [TTransportException exceptionWithName: @"TTransportException"
-                                           reason: [NSString stringWithFormat: @"Unexpected NSURLResponse type: %@",
-                                                    NSStringFromClass([response class])]];
-  }
-
-  NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
-  if ([httpResponse statusCode] != 200) {
-    @throw [TTransportException exceptionWithName: @"TTransportException"
-                                           reason: [NSString stringWithFormat: @"Bad response from HTTP server: %ld",
-                                                    (long)[httpResponse statusCode]]];
-  }
-
-  // phew!
-  [mResponseData release_stub];
-  mResponseData = [responseData retain_stub];
-  mResponseDataOffset = 0;
-}
-
-
-@end
diff --git a/lib/cocoa/src/transport/THTTPSessionTransport.h b/lib/cocoa/src/transport/THTTPSessionTransport.h
new file mode 100644
index 0000000..003499b
--- /dev/null
+++ b/lib/cocoa/src/transport/THTTPSessionTransport.h
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#import <Foundation/Foundation.h>
+#import "TAsyncTransport.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+
+typedef NSError *__nullable (^THTTPSessionTransportResponseValidateBlock) (NSHTTPURLResponse *response, NSData *responseData);
+
+
+@interface THTTPSessionTransportFactory : NSObject<TAsyncTransportFactory>
+
+@property (strong, nonatomic) THTTPSessionTransportResponseValidateBlock responseValidate;
+
++(void) setupDefaultsForSessionConfiguration:(NSURLSessionConfiguration *)config
+                            withProtocolName:(NSString *)protocolName;
+
+-(id) initWithSession:(NSURLSession *)session
+                  URL:(NSURL *)aURL;
+
+@end
+
+
+@interface THTTPSessionTransport : NSObject <TAsyncTransport>
+
+@end
+
+
+NS_ASSUME_NONNULL_END
diff --git a/lib/cocoa/src/transport/THTTPSessionTransport.m b/lib/cocoa/src/transport/THTTPSessionTransport.m
new file mode 100644
index 0000000..c10b7fc
--- /dev/null
+++ b/lib/cocoa/src/transport/THTTPSessionTransport.m
@@ -0,0 +1,268 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#import "THTTPSessionTransport.h"
+#import "TTransportError.h"
+
+
+@interface THTTPSessionTransportFactory ()
+
+@property (strong, nonatomic) NSURLSession *session;
+@property (strong, nonatomic) NSURL *url;
+
+@end
+
+
+@interface THTTPSessionTransport ()
+
+@property (strong, nonatomic) THTTPSessionTransportFactory *factory;
+@property (strong, nonatomic) NSMutableData *requestData;
+@property (strong, nonatomic) NSData *responseData;
+@property (assign, nonatomic) NSUInteger responseDataOffset;
+
+-(instancetype) initWithFactory:(THTTPSessionTransportFactory *)factory;
+
+@end
+
+
+@implementation THTTPSessionTransportFactory
+
++(void) setupDefaultsForSessionConfiguration:(NSURLSessionConfiguration *)config withProtocolName:(NSString *)protocolName
+{
+  NSString *thriftContentType = @"application/x-thrift";
+  if (protocolName.length) {
+    thriftContentType = [thriftContentType stringByAppendingFormat:@"; p=%@", protocolName];
+  }
+
+  config.requestCachePolicy = NSURLRequestReloadIgnoringCacheData;
+  config.HTTPShouldUsePipelining = YES;
+  config.HTTPShouldSetCookies = NO;
+  config.URLCache = nil;
+  config.HTTPAdditionalHeaders = @{@"Content-Type":thriftContentType,
+                                   @"Accept":thriftContentType,
+                                   @"User-Agent":@"Thrift/Cocoa (Session)"};
+}
+
+
+-(id) initWithSession:(NSURLSession *)session URL:(NSURL *)url
+{
+  self = [super init];
+  if (self) {
+    _session = session;
+    _url = url;
+  }
+
+  return self;
+}
+
+-(id<TAsyncTransport>) newTransport
+{
+  return [[THTTPSessionTransport alloc] initWithFactory:self];
+}
+
+-(NSURLSessionDataTask *) taskWithRequest:(NSURLRequest *)request
+                        completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler
+                                    error:(NSError *__autoreleasing *)error
+{
+  NSURLSessionDataTask *newTask = [_session dataTaskWithRequest:request completionHandler:completionHandler];
+  if (!newTask) {
+    if (error) {
+      *error = [NSError errorWithDomain:TTransportErrorDomain
+                                   code:TTransportErrorUnknown
+                               userInfo:@{NSLocalizedDescriptionKey:@"Failed to create session data task"}];
+    }
+    return nil;
+  }
+
+  return newTask;
+}
+
+-(NSError *) validateResponse:(NSHTTPURLResponse *)response data:(NSData *)data
+{
+  if (_responseValidate) {
+    return _responseValidate(response, data);
+  }
+  return nil;
+}
+
+@end
+
+
+
+@implementation THTTPSessionTransport
+
+-(instancetype) initWithFactory:(THTTPSessionTransportFactory *)factory
+{
+  self = [super init];
+  if (self) {
+    _factory = factory;
+  }
+  return self;
+}
+
+-(BOOL) readAll:(UInt8 *)outBuffer offset:(UInt32)outBufferOffset length:(UInt32)length error:(NSError *__autoreleasing *)error
+{
+  UInt32 got = [self readAvail:outBuffer offset:outBufferOffset maxLength:length error:error];
+  if (got != length) {
+
+    // Report underflow only if readAvail didn't report error already
+    if (error && !*error) {
+      *error = [NSError errorWithDomain:TTransportErrorDomain
+                                   code:TTransportErrorEndOfFile
+                               userInfo:nil];
+    }
+
+    return NO;
+  }
+
+  return YES;
+}
+
+-(UInt32) readAvail:(UInt8 *)outBuffer offset:(UInt32)outBufferOffset maxLength:(UInt32)maxLength error:(NSError *__autoreleasing *)error
+{
+  NSUInteger avail = _responseData.length - _responseDataOffset;
+
+  NSRange range;
+  range.location = _responseDataOffset;
+  range.length = MIN(maxLength, avail);
+
+  [_responseData getBytes:outBuffer+outBufferOffset range:range];
+  _responseDataOffset += range.length;
+
+  return (UInt32)range.length;
+}
+
+-(BOOL) write:(const UInt8 *)data offset:(UInt32)offset length:(UInt32)length error:(NSError *__autoreleasing *)error
+{
+  if (!_requestData) {
+    _requestData = [NSMutableData dataWithCapacity:256];
+  }
+
+  [_requestData appendBytes:data+offset length:length];
+
+  return YES;
+}
+
+-(void) flushWithCompletion:(TAsyncCompletionBlock)completed failure:(TAsyncFailureBlock)failure
+{
+  NSError *error;
+
+  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:_factory.url];
+  request.HTTPMethod = @"POST";
+  request.HTTPBody = _requestData;
+
+  _requestData = nil;
+
+  NSURLSessionDataTask *task = [_factory taskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
+
+    // Check response type
+    if (!error && ![response isKindOfClass:NSHTTPURLResponse.class]) {
+
+      error = [NSError errorWithDomain:TTransportErrorDomain
+                                   code:TTransportErrorUnknown
+                               userInfo:@{TTransportErrorHttpErrorKey: @(THttpTransportErrorInvalidResponse)}];
+
+    }
+
+    // Check status code
+    NSHTTPURLResponse *httpResponse = (id)response;
+    if (!error && httpResponse.statusCode != 200) {
+
+      THttpTransportError code;
+      if (httpResponse.statusCode == 401) {
+        code = THttpTransportErrorAuthentication;
+      }
+      else {
+        code = THttpTransportErrorInvalidStatus;
+      }
+
+      error = [NSError errorWithDomain:TTransportErrorDomain
+                                  code:TTransportErrorUnknown
+                              userInfo:@{TTransportErrorHttpErrorKey: @(code),
+                                         @"statusCode":@(httpResponse.statusCode)}];
+    }
+
+    // Allow factory to check
+    if (!error) {
+      error = [_factory validateResponse:httpResponse data:data];
+    }
+
+    _responseDataOffset = 0;
+
+    if (error) {
+
+      _responseData = nil;
+
+      failure(error);
+
+    }
+    else {
+
+      if (data == nil) {
+        data = [NSData data];
+      }
+
+      _responseData = data;
+
+      completed(self);
+    }
+
+  } error:&error];
+
+  if (!task) {
+    failure(error);
+    return;
+  }
+
+  [task resume];
+}
+
+-(BOOL) flush:(NSError *__autoreleasing *)error
+{
+  dispatch_semaphore_t completed = dispatch_semaphore_create(0);
+
+  __block BOOL result;
+  __block NSError *internalError;
+
+  [self flushWithCompletion:^(id < TAsyncTransport > transport) {
+
+    result = YES;
+
+    dispatch_semaphore_signal(completed);
+
+  } failure:^(NSError *error) {
+
+    internalError = error;
+
+    result = NO;
+
+    dispatch_semaphore_signal(completed);
+
+  }];
+
+  dispatch_semaphore_wait(completed, DISPATCH_TIME_FOREVER);
+
+  if (error) {
+    *error = internalError;
+  }
+
+  return result;
+}
+
+@end
diff --git a/lib/cocoa/src/transport/TSocketClient.h b/lib/cocoa/src/transport/THTTPTransport.h
similarity index 73%
copy from lib/cocoa/src/transport/TSocketClient.h
copy to lib/cocoa/src/transport/THTTPTransport.h
index 81a0247..3c35daf 100644
--- a/lib/cocoa/src/transport/TSocketClient.h
+++ b/lib/cocoa/src/transport/THTTPTransport.h
@@ -18,19 +18,22 @@
  */
 
 #import <Foundation/Foundation.h>
-#import "TNSStreamTransport.h"
+#import "TTransport.h"
 
-@interface TSocketClient : TNSStreamTransport 
-#if TARGET_OS_IPHONE || (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
-<NSStreamDelegate>
-#endif
-{
-}
+NS_ASSUME_NONNULL_BEGIN
 
-- (id) initWithHostname: (NSString *) hostname
-                   port: (UInt32) port;
+
+@interface THTTPTransport : NSObject <TTransport>
+
+-(id) initWithURL:(NSURL *)aURL;
+
+-(id) initWithURL:(NSURL *)aURL
+        userAgent:(nullable NSString *)userAgent
+          timeout:(int)timeout;
+
+-(void) setURL:(NSURL *)aURL;
 
 @end
 
 
-
+NS_ASSUME_NONNULL_END
diff --git a/lib/cocoa/src/transport/THTTPTransport.m b/lib/cocoa/src/transport/THTTPTransport.m
new file mode 100644
index 0000000..e4046c6
--- /dev/null
+++ b/lib/cocoa/src/transport/THTTPTransport.m
@@ -0,0 +1,182 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#import "THTTPTransport.h"
+#import "TTransportError.h"
+
+
+@interface THTTPTransport ()
+
+@property (strong, nonatomic) NSURL *url;
+@property (strong, nonatomic) NSMutableURLRequest *request;
+@property (strong, nonatomic) NSMutableData *requestData;
+@property (strong, nonatomic) NSData *responseData;
+@property (assign, nonatomic) NSUInteger responseDataOffset;
+@property (strong, nonatomic) NSString *userAgent;
+@property (assign, nonatomic) NSTimeInterval timeout;
+
+@end
+
+
+@implementation THTTPTransport
+
+-(void) setupRequest
+{
+  // set up our request object that we'll use for each request
+  _request = [[NSMutableURLRequest alloc] initWithURL:_url];
+  [_request setHTTPMethod:@"POST"];
+  [_request setValue:@"application/x-thrift" forHTTPHeaderField:@"Content-Type"];
+  [_request setValue:@"application/x-thrift" forHTTPHeaderField:@"Accept"];
+
+  NSString *userAgent = _userAgent;
+  if (!userAgent) {
+    userAgent = @"Thrift/Cocoa";
+  }
+  [_request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
+
+  [_request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
+  if (_timeout) {
+    [_request setTimeoutInterval:_timeout];
+  }
+}
+
+
+-(id) initWithURL:(NSURL *)aURL
+{
+  return [self initWithURL:aURL
+                 userAgent:nil
+                   timeout:0];
+}
+
+
+-(id) initWithURL:(NSURL *)aURL
+        userAgent:(NSString *)aUserAgent
+          timeout:(int)aTimeout
+{
+  self = [super init];
+  if (!self) {
+    return nil;
+  }
+
+  _timeout = aTimeout;
+  _userAgent = aUserAgent;
+  _url = aURL;
+
+  [self setupRequest];
+
+  // create our request data buffer
+  _requestData = [[NSMutableData alloc] initWithCapacity:1024];
+
+  return self;
+}
+
+-(void) setURL:(NSURL *)aURL
+{
+  _url = aURL;
+
+  [self setupRequest];
+}
+
+-(BOOL) readAll:(UInt8 *)outBuffer offset:(UInt32)outBufferOffset length:(UInt32)length error:(NSError *__autoreleasing *)error
+{
+  UInt32 got = [self readAvail:outBuffer offset:outBufferOffset maxLength:length error:error];
+  if (got != length) {
+
+    // Report underflow only if readAvail didn't report error already
+    if (error && !*error) {
+      *error = [NSError errorWithDomain:TTransportErrorDomain
+                                   code:TTransportErrorEndOfFile
+                               userInfo:nil];
+    }
+
+    return NO;
+  }
+
+  return YES;
+}
+
+-(UInt32) readAvail:(UInt8 *)outBuffer offset:(UInt32)outBufferOffset maxLength:(UInt32)maxLength error:(NSError *__autoreleasing *)error
+{
+  NSUInteger avail = _responseData.length - _responseDataOffset;
+
+  NSRange range;
+  range.location = _responseDataOffset;
+  range.length = MIN(maxLength, avail);
+
+  [_responseData getBytes:outBuffer+outBufferOffset range:range];
+  _responseDataOffset += range.length;
+
+  return (UInt32)range.length;
+}
+
+-(BOOL) write:(const UInt8 *)data offset:(UInt32)offset length:(UInt32)length error:(NSError *__autoreleasing *)error
+{
+  [_requestData appendBytes:data+offset length:length];
+
+  return YES;
+}
+
+-(BOOL) flush:(NSError *__autoreleasing *)error
+{
+  [_request setHTTPBody:_requestData];
+
+  _responseDataOffset = 0;
+
+  // make the HTTP request
+  NSURLResponse *response;
+  _responseData = [NSURLConnection sendSynchronousRequest:_request returningResponse:&response error:error];
+  if (!_responseData) {
+    return NO;
+  }
+
+  [_requestData setLength:0];
+
+  if (![response isKindOfClass:NSHTTPURLResponse.class]) {
+    if (error) {
+      *error = [NSError errorWithDomain:TTransportErrorDomain
+                                   code:TTransportErrorUnknown
+                               userInfo:@{TTransportErrorHttpErrorKey: @(THttpTransportErrorInvalidResponse)}];
+    }
+    return NO;
+  }
+
+  NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
+  if ([httpResponse statusCode] != 200) {
+    if (error) {
+
+      THttpTransportError code;
+      if (httpResponse.statusCode == 401) {
+        code = THttpTransportErrorAuthentication;
+      }
+      else {
+        code = THttpTransportErrorInvalidStatus;
+      }
+
+      *error = [NSError errorWithDomain:TTransportErrorDomain
+                                   code:TTransportErrorUnknown
+                               userInfo:@{TTransportErrorHttpErrorKey: @(code),
+                                          @"statusCode":@(httpResponse.statusCode)}];
+    }
+    return NO;
+  }
+
+  return YES;
+}
+
+@end
diff --git a/lib/cocoa/src/transport/TMemoryBuffer.h b/lib/cocoa/src/transport/TMemoryBuffer.h
index fa4d371..6249d32 100644
--- a/lib/cocoa/src/transport/TMemoryBuffer.h
+++ b/lib/cocoa/src/transport/TMemoryBuffer.h
@@ -20,10 +20,18 @@
 #import <Foundation/Foundation.h>
 #import "TTransport.h"
 
-@interface TMemoryBuffer : NSObject <TTransport> {
-	NSMutableData *mBuffer;
-	NSUInteger mOffset;
-}
-- (id)initWithData:(NSData *)data;
-- (NSData *)getBuffer;
+NS_ASSUME_NONNULL_BEGIN
+
+
+@interface TMemoryBuffer : NSObject <TTransport>
+
+-(NSData *) buffer;
+
+-(id) initWithData:(NSData *)data;
+
+-(id) initWithDataNoCopy:(NSMutableData *)data;
+
 @end
+
+
+NS_ASSUME_NONNULL_END
diff --git a/lib/cocoa/src/transport/TMemoryBuffer.m b/lib/cocoa/src/transport/TMemoryBuffer.m
index 4513ab8..ec19cc8 100644
--- a/lib/cocoa/src/transport/TMemoryBuffer.m
+++ b/lib/cocoa/src/transport/TMemoryBuffer.m
@@ -18,57 +18,104 @@
  */
 
 #import "TMemoryBuffer.h"
-#import "TTransportException.h"
-#import "TObjective-C.h"
+#import "TTransportError.h"
+
 
 #define GARBAGE_BUFFER_SIZE 4096 // 4KiB
 
+
+@interface TMemoryBuffer ()
+
+@property(strong, nonatomic) NSMutableData *buffer;
+@property(assign, nonatomic) UInt32 bufferOffset;
+
+@end
+
+
 @implementation TMemoryBuffer
-- (id)init {
-	if ((self = [super init])) {
-		mBuffer = [[NSMutableData alloc] init];
-		mOffset = 0;
-	}
-	return self;
-}
 
-- (id)initWithData:(NSData *)data {
-	if ((self = [super init])) {
-		mBuffer = [data mutableCopy];
-		mOffset = 0;
-	}
-	return self;
-}
-
-- (size_t) readAll: (uint8_t *) buf offset: (size_t) offset length: (size_t) length
+-(id) init
 {
-	if ([mBuffer length] - mOffset < length) {
-		@throw [TTransportException exceptionWithReason:@"Not enough bytes remain in buffer"];
-	}
-	[mBuffer getBytes:buf range:NSMakeRange(mOffset, length)];
-	mOffset += length;
-	if (mOffset >= GARBAGE_BUFFER_SIZE) {
-		[mBuffer replaceBytesInRange:NSMakeRange(0, mOffset) withBytes:NULL length:0];
-		mOffset = 0;
-	}
-	return length;
+  if ((self = [super init])) {
+    _buffer = [NSMutableData new];
+    _bufferOffset = 0;
+  }
+  return self;
 }
 
-- (void) write: (const uint8_t *) data offset: (size_t) offset length: (size_t) length
+-(id) initWithData:(NSData *)data
 {
-	[mBuffer appendBytes:data+offset length:length];
+  if (self = [super init]) {
+    _buffer = [data mutableCopy];
+    _bufferOffset = 0;
+  }
+  return self;
 }
 
-- (void)flush {
-	// noop
+-(id) initWithDataNoCopy:(NSMutableData *)data
+{
+  if (self = [super init]) {
+    _buffer = data;
+    _bufferOffset = 0;
+  }
+  return self;
 }
 
-- (NSData *)getBuffer {
-	return [[mBuffer copy] autorelease_stub];
+-(BOOL) readAll:(UInt8 *)outBuffer offset:(UInt32)outBufferOffset length:(UInt32)length error:(NSError *__autoreleasing *)error
+{
+  UInt32 got = [self readAvail:outBuffer offset:outBufferOffset maxLength:length error:error];
+  if (got != length) {
+
+    // Report underflow only if readAvail didn't report error already
+    if (error && !*error) {
+      *error = [NSError errorWithDomain:TTransportErrorDomain
+                                   code:TTransportErrorEndOfFile
+                               userInfo:nil];
+    }
+
+    return NO;
+  }
+
+  return YES;
 }
 
-- (void)dealloc {
-	[mBuffer release_stub];
-	[super dealloc_stub];
+-(UInt32) readAvail:(UInt8 *)outBuffer offset:(UInt32)outBufferOffset maxLength:(UInt32)maxLength error:(NSError *__autoreleasing *)error
+{
+  UInt32 avail = (UInt32)_buffer.length - _bufferOffset;
+  if (avail == 0) {
+    return 0;
+  }
+
+  NSRange range;
+  range.location = _bufferOffset;
+  range.length = MIN(maxLength, avail);
+
+  [_buffer getBytes:outBuffer + outBufferOffset range:range];
+  _bufferOffset += range.length;
+
+  if (_bufferOffset >= GARBAGE_BUFFER_SIZE) {
+    [_buffer replaceBytesInRange:NSMakeRange(0, _bufferOffset) withBytes:NULL length:0];
+    _bufferOffset = 0;
+  }
+
+  return (UInt32)range.length;
 }
+
+-(BOOL) write:(const UInt8 *)inBuffer offset:(UInt32)inBufferOffset length:(UInt32)length error:(NSError *__autoreleasing *)error
+{
+  [_buffer appendBytes:inBuffer + inBufferOffset length:length];
+
+  return YES;
+}
+
+-(NSData *) buffer
+{
+  return _buffer;
+}
+
+-(BOOL) flush:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+
 @end
diff --git a/lib/cocoa/src/transport/TNSFileHandleTransport.h b/lib/cocoa/src/transport/TNSFileHandleTransport.h
index ba2a209..db6edf3 100644
--- a/lib/cocoa/src/transport/TNSFileHandleTransport.h
+++ b/lib/cocoa/src/transport/TNSFileHandleTransport.h
@@ -21,15 +21,18 @@
 #import <Foundation/Foundation.h>
 #import "TTransport.h"
 
-@interface TNSFileHandleTransport : NSObject <TTransport> {
-  NSFileHandle * mInputFileHandle;
-  NSFileHandle * mOutputFileHandle;
-}
+NS_ASSUME_NONNULL_BEGIN
 
-- (id) initWithFileHandle: (NSFileHandle *) fileHandle;
 
-- (id) initWithInputFileHandle: (NSFileHandle *) inputFileHandle
-              outputFileHandle: (NSFileHandle *) outputFileHandle;
+@interface TNSFileHandleTransport : NSObject <TTransport>
+
+-(id) initWithFileHandle:(NSFileHandle *)fileHandle;
+
+-(id) initWithInputFileHandle:(NSFileHandle *)inputFileHandle
+             outputFileHandle:(NSFileHandle *)outputFileHandle;
 
 
 @end
+
+
+NS_ASSUME_NONNULL_END
diff --git a/lib/cocoa/src/transport/TNSFileHandleTransport.m b/lib/cocoa/src/transport/TNSFileHandleTransport.m
index c2b18ca..c907f87 100644
--- a/lib/cocoa/src/transport/TNSFileHandleTransport.m
+++ b/lib/cocoa/src/transport/TNSFileHandleTransport.m
@@ -19,75 +19,100 @@
 
 
 #import "TNSFileHandleTransport.h"
-#import "TTransportException.h"
-#import "TObjective-C.h"
+#import "TTransportError.h"
+
+
+@interface TNSFileHandleTransport ()
+
+@property(strong, nonatomic) NSFileHandle *inputFileHandle;
+@property(strong, nonatomic) NSFileHandle *outputFileHandle;
+
+@end
 
 
 @implementation TNSFileHandleTransport
 
-- (id) initWithFileHandle: (NSFileHandle *) fileHandle
+-(id) initWithFileHandle:(NSFileHandle *)fileHandle
 {
-  return [self initWithInputFileHandle: fileHandle
-                      outputFileHandle: fileHandle];
+  return [self initWithInputFileHandle:fileHandle
+                      outputFileHandle:fileHandle];
 }
 
 
-- (id) initWithInputFileHandle: (NSFileHandle *) inputFileHandle
-              outputFileHandle: (NSFileHandle *) outputFileHandle
+-(id) initWithInputFileHandle:(NSFileHandle *)aInputFileHandle
+             outputFileHandle:(NSFileHandle *)aOutputFileHandle
 {
   self = [super init];
-
-  mInputFileHandle = [inputFileHandle retain_stub];
-  mOutputFileHandle = [outputFileHandle retain_stub];
-
+  if (self) {
+    _inputFileHandle = aInputFileHandle;
+    _outputFileHandle = aOutputFileHandle;
+  }
   return self;
 }
 
 
-- (void) dealloc {
-  [mInputFileHandle release_stub];
-  [mOutputFileHandle release_stub];
-  [super dealloc_stub];
-}
-
-
-- (size_t) readAll: (uint8_t *) buf offset: (size_t) offset length: (size_t) length
+-(BOOL) readAll:(UInt8 *)buf offset:(UInt32)off length:(UInt32)len error:(NSError *__autoreleasing *)error
 {
-  size_t totalBytesRead = 0;
-  while (totalBytesRead < length) {
-    NSData * data = [mInputFileHandle readDataOfLength: length-totalBytesRead];
-    if ([data length] == 0) {
-      @throw [TTransportException exceptionWithName: @"TTransportException"
-                                  reason: @"Cannot read. No more data."];
+  UInt32 got = 0;
+  while (got < len) {
+
+    NSData *d = [_inputFileHandle readDataOfLength:len-got];
+    if (d.length == 0) {
+      if (error) {
+        *error = [NSError errorWithDomain:TTransportErrorDomain
+                                     code:TTransportErrorEndOfFile
+                                 userInfo:nil];
+      }
+      return NO;
     }
-    [data getBytes: buf+totalBytesRead];
-    totalBytesRead += [data length];
+
+    [d getBytes:buf+got length:d.length];
+    got += d.length;
   }
-  return totalBytesRead;
+  return YES;
 }
 
 
-- (void) write: (const uint8_t *) data offset: (size_t) offset length: (size_t) length
+-(UInt32) readAvail:(UInt8 *)buf offset:(UInt32)off maxLength:(UInt32)len error:(NSError *__autoreleasing *)error
 {
-  const void *pos = data + offset;
-  NSData * dataObject = [[NSData alloc] initWithBytesNoCopy: (void *)pos
-                                                     length: length
-                                               freeWhenDone: NO];
+  UInt32 got = 0;
+  while (got < len) {
+
+    NSData *d = [_inputFileHandle readDataOfLength:len-got];
+    if (d.length == 0) {
+      break;
+    }
+
+    [d getBytes:buf+got length:d.length];
+    got += d.length;
+  }
+  return got;
+}
+
+
+-(BOOL) write:(const UInt8 *)data offset:(UInt32)offset length:(UInt32)length error:(NSError *__autoreleasing *)error
+{
+  void *pos = (void *)data + offset;
 
   @try {
-    [mOutputFileHandle writeData: dataObject];
-  } @catch (NSException * e) {
-    @throw [TTransportException exceptionWithName: @"TTransportException"
-                                           reason: [NSString stringWithFormat: @"%s: Unable to write data: %@", __PRETTY_FUNCTION__, e]];
+    [_outputFileHandle writeData:[NSData dataWithBytesNoCopy:pos length:length freeWhenDone:NO]];
+  }
+  @catch (NSException *e) {
+    if (error) {
+      *error = [NSError errorWithDomain:TTransportErrorDomain
+                                   code:TTransportErrorNotOpen
+                               userInfo:@{}];
+    }
+    return NO;
   }
 
-  [dataObject release_stub];
+  return YES;
 }
 
 
-- (void) flush
+-(BOOL) flush:(NSError *__autoreleasing *)error
 {
-
+  return YES;
 }
 
 @end
diff --git a/lib/cocoa/src/transport/TNSStreamTransport.h b/lib/cocoa/src/transport/TNSStreamTransport.h
index 8011fb9..54c4884 100644
--- a/lib/cocoa/src/transport/TNSStreamTransport.h
+++ b/lib/cocoa/src/transport/TNSStreamTransport.h
@@ -20,21 +20,24 @@
 #import <Foundation/Foundation.h>
 #import "TTransport.h"
 
-@interface TNSStreamTransport : NSObject <TTransport> {
+NS_ASSUME_NONNULL_BEGIN
 
-}
 
-@property (nonatomic, strong) NSInputStream * mInput;
-@property (nonatomic, strong) NSOutputStream * mOutput;
+@interface TNSStreamTransport : NSObject <TTransport>
 
-- (id) initWithInputStream: (NSInputStream *) input
-              outputStream: (NSOutputStream *) output;
+@property (strong, nonatomic) NSInputStream *input;
+@property (strong, nonatomic) NSOutputStream *output;
 
-- (id) initWithInputStream: (NSInputStream *) input;
+-(id) initWithInputStream:(nullable NSInputStream *)input
+             outputStream:(nullable NSOutputStream *)output;
 
-- (id) initWithOutputStream: (NSOutputStream *) output;
+-(id) initWithInputStream:(NSInputStream *)input;
+
+-(id) initWithOutputStream:(NSOutputStream *)output;
+
+-(void) close;
 
 @end
 
 
-
+NS_ASSUME_NONNULL_END
diff --git a/lib/cocoa/src/transport/TNSStreamTransport.m b/lib/cocoa/src/transport/TNSStreamTransport.m
index 7ac1cdc..c425043 100644
--- a/lib/cocoa/src/transport/TNSStreamTransport.m
+++ b/lib/cocoa/src/transport/TNSStreamTransport.m
@@ -18,79 +18,136 @@
  */
 
 #import "TNSStreamTransport.h"
-#import "TTransportException.h"
-#import "TObjective-C.h"
+#import "TTransportError.h"
+
+
+@interface TNSStreamTransport ()
+@end
 
 
 @implementation TNSStreamTransport
 
-- (id) initWithInputStream: (NSInputStream *) input
-              outputStream: (NSOutputStream *) output
+-(id) initWithInputStream:(NSInputStream *)input
+             outputStream:(NSOutputStream *)output
 {
   self = [super init];
-  self.mInput = [input retain_stub];
-  self.mOutput = [output retain_stub];
+  if (self) {
+    _input = input;
+    _output = output;
+  }
   return self;
 }
 
-- (id) initWithInputStream: (NSInputStream *) input
+-(id) initWithInputStream:(NSInputStream *)input
 {
-  return [self initWithInputStream: input outputStream: nil];
+  return [self initWithInputStream:input outputStream:nil];
 }
 
-- (id) initWithOutputStream: (NSOutputStream *) output
+-(id) initWithOutputStream:(NSOutputStream *)output
 {
-  return [self initWithInputStream: nil outputStream: output];
+  return [self initWithInputStream:nil outputStream:output];
 }
 
-- (void) dealloc
+-(void) dealloc
 {
-  [self.mInput release_stub];
-  [self.mOutput release_stub];
-  [super dealloc_stub];
+  [self close];
 }
 
-
-- (size_t) readAll: (uint8_t *) buf offset: (size_t) offset length: (size_t) length
+-(BOOL) readAll:(UInt8 *)buf offset:(UInt32)off length:(UInt32)len error:(NSError *__autoreleasing *)error
 {
-  size_t totalBytesRead = 0;
-  ssize_t bytesRead = 0;
-  while (totalBytesRead < length) {
-    bytesRead = [self.mInput read: buf+offset+totalBytesRead maxLength: length-totalBytesRead];
+  UInt32 got = 0;
+  while (got < len) {
 
-    BOOL encounteredErrorOrEOF = (bytesRead <= 0);
-    if (encounteredErrorOrEOF) {
-      @throw [TTransportException exceptionWithReason: @"Cannot read. Remote side has closed."];
-    } else {
-        /* bytesRead is guaranteed to be positive and within the range representable by size_t. */
-        totalBytesRead += (size_t)bytesRead;
+    UInt32 read = (UInt32)[_input read:buf+off+got maxLength:len-got];
+    if (read <= 0) {
+      if (error) {
+        *error = [NSError errorWithDomain:TTransportErrorDomain
+                                     code:TTransportErrorNotOpen
+                                 userInfo:@{}];
+      }
+      return NO;
     }
+
+    got += read;
   }
-  return totalBytesRead;
+
+  return YES;
 }
 
 
-- (void) write: (const uint8_t *) data offset: (size_t) offset length: (size_t) length
+-(UInt32) readAvail:(UInt8 *)buf offset:(UInt32)off maxLength:(UInt32)len error:(NSError *__autoreleasing *)error
 {
-  size_t totalBytesWritten = 0;
-  ssize_t bytesWritten = 0;
-  while (totalBytesWritten < length) {
-    bytesWritten = [self.mOutput write: data+offset+totalBytesWritten maxLength: length-totalBytesWritten];
-    if (bytesWritten < 0) {
-      @throw [TTransportException exceptionWithReason: @"Error writing to transport output stream."
-                                                error: [self.mOutput streamError]];
-    } else if (bytesWritten == 0) {
-      @throw [TTransportException exceptionWithReason: @"End of output stream."];
-    } else {
-        /* bytesWritten is guaranteed to be positive and within the range representable by size_t. */
-        totalBytesWritten += (size_t)bytesWritten;
+  UInt32 got = 0;
+  while (got < len) {
+
+    UInt32 read = (UInt32)[_input read:buf+off+got maxLength:len-got];
+    if (read <= 0) {
+      break;
     }
+
+    got += read;
   }
+
+  return got;
 }
 
-- (void) flush
+
+-(BOOL) write:(const UInt8 *)data offset:(UInt32)offset length:(UInt32)length error:(NSError *__autoreleasing *)error
 {
-  // no flush for you!
+  int got = 0;
+  NSInteger total = 0;
+  while (got < length) {
+
+    total = [_output write:data+offset+got maxLength:length-got];
+    if (total == -1) {
+      if (error) {
+        *error = [NSError errorWithDomain:TTransportErrorDomain
+                                     code:TTransportErrorNotOpen
+                                 userInfo:@{}];
+      }
+      return NO;
+    }
+    else if (total == 0) {
+      if (error) {
+        *error = [NSError errorWithDomain:TTransportErrorDomain
+                                     code:TTransportErrorEndOfFile
+                                 userInfo:@{}];
+      }
+      return NO;
+    }
+
+    got += total;
+  }
+
+  return YES;
+}
+
+-(BOOL) flush:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+
+-(void) close
+{
+  NSInputStream *input = self.input;
+  if (input) {
+    // Close and reset inputstream
+    CFReadStreamSetProperty((__bridge CFReadStreamRef)(input), kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
+    [input setDelegate:nil];
+    [input close];
+    [input removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
+    input = nil;
+  }
+
+  NSOutputStream *output = self.output;
+  if (output) {
+    // Close and reset outputstream
+    CFWriteStreamSetProperty((__bridge CFWriteStreamRef)(output), kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
+    [output setDelegate:nil];
+    [output close];
+    [output removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
+    output = nil;
+  }
 }
 
 @end
diff --git a/lib/cocoa/src/transport/TSSLSocketClient.h b/lib/cocoa/src/transport/TSSLSocketClient.h
deleted file mode 100644
index 44de124..0000000
--- a/lib/cocoa/src/transport/TSSLSocketClient.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#import <Foundation/Foundation.h>
-#import "TNSStreamTransport.h"
-
-@interface TSSLSocketClient : TNSStreamTransport
-#if TARGET_OS_IPHONE || (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
-<NSStreamDelegate>
-#endif
-{
-    NSInputStream *inputStream;
-    NSOutputStream *outputStream;
-@private
-    NSString *sslHostname;
-    int sd;
-}
-
-- (id) initWithHostname: (NSString *) hostname
-                   port: (int) port;
-
-- (BOOL) isOpen;
-
-@end
diff --git a/lib/cocoa/src/transport/TSSLSocketClient.m b/lib/cocoa/src/transport/TSSLSocketClient.m
deleted file mode 100644
index d8c55d6..0000000
--- a/lib/cocoa/src/transport/TSSLSocketClient.m
+++ /dev/null
@@ -1,261 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-#import <Foundation/Foundation.h>
-#import <CoreFoundation/CoreFoundation.h>
-#import "TSSLSocketClient.h"
-#import "TSSLSocketException.h"
-#import "TObjective-C.h"
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-
-#if !TARGET_OS_IPHONE
-#import <CoreServices/CoreServices.h>
-#else
-#import <CFNetwork/CFNetwork.h>
-#endif
-
-@implementation TSSLSocketClient
-
-- (id) initWithHostname: (NSString *) hostname
-                   port: (int) port
-{
-    sslHostname = hostname;
-	CFReadStreamRef readStream = NULL;
-	CFWriteStreamRef writeStream = NULL;
-    
-    
-    /* create a socket structure */
-    struct sockaddr_in pin;
-    struct hostent *hp = NULL;
-    for(int i = 0; i < 10; i++) {
-        
-
-        
-        if ((hp = gethostbyname([hostname UTF8String])) == NULL) { 
-            NSLog(@"failed to resolve hostname %@", hostname);
-            herror("resolv");
-            if(i == 9) {
-                @throw [TSSLSocketException exceptionWithReason: @"failed to resolve hostname"];
-            }
-            [NSThread sleepForTimeInterval:0.2];
-        } else {
-            break;
-        }
-    }
-
-    memset (&pin, 0, sizeof(pin));
-    pin.sin_family = AF_INET;
-    memcpy(&pin.sin_addr, hp->h_addr, sizeof(struct in_addr));
-    pin.sin_port = htons (port);
-    
-    /* create the socket */
-    if ((sd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
-    {
-        NSLog(@"failed to create socket for host %@:%d", hostname, port);
-        @throw [TSSLSocketException exceptionWithReason: @"failed to create socket"];
-    }
-    
-    /* open a connection */
-    if (connect (sd, (struct sockaddr *) &pin, sizeof(pin)) == -1)
-    {
-        NSLog(@"failed to create conenct to host %@:%d", hostname, port);
-        @throw [TSSLSocketException exceptionWithReason: @"failed to connect"];
-    }
-    CFStreamCreatePairWithSocket(kCFAllocatorDefault, sd, &readStream, &writeStream);
-    
-    CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
-    CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
-    
-	if (readStream && writeStream) {
-        CFReadStreamSetProperty(readStream,
-                                kCFStreamPropertySocketSecurityLevel,
-                                kCFStreamSocketSecurityLevelTLSv1);
-        
-        NSDictionary *settings =
-        [NSDictionary dictionaryWithObjectsAndKeys:
-         (id)kCFBooleanTrue, (id)kCFStreamSSLValidatesCertificateChain,
-         nil];
-        
-        CFReadStreamSetProperty((CFReadStreamRef)readStream,
-                                kCFStreamPropertySSLSettings,
-                                (CFTypeRef)settings);
-        CFWriteStreamSetProperty((CFWriteStreamRef)writeStream,
-                                 kCFStreamPropertySSLSettings,
-                                 (CFTypeRef)settings);
-        
-		inputStream = (bridge_stub NSInputStream *)readStream;
-		[inputStream retain_stub];
-		[inputStream setDelegate:self];
-   		[inputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
-		[inputStream open];
-		
-		outputStream = (bridge_stub NSOutputStream *)writeStream;
-		[outputStream retain_stub];
-		[outputStream setDelegate:self];
-        [outputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
-		[outputStream open];
-        
-        
-        CFRelease(readStream);
-        CFRelease(writeStream);
-	}
-    
-    
-	
-	self = [super initWithInputStream: inputStream outputStream: outputStream];
-    
-	return self;
-}
-
-#pragma mark -
-#pragma mark NSStreamDelegate
-- (void)stream:(NSStream *)aStream
-    handleEvent:(NSStreamEvent)eventCode {
-    switch (eventCode) {
-        case NSStreamEventNone:
-            break;
-        case NSStreamEventHasBytesAvailable:
-            break;
-        case NSStreamEventOpenCompleted:
-            break;
-        case NSStreamEventHasSpaceAvailable:
-        {
-            SecPolicyRef policy = SecPolicyCreateSSL(NO, (__bridge CFStringRef)(sslHostname));
-            SecTrustRef trust = NULL;
-            CFArrayRef streamCertificatesRef =
-            CFBridgingRetain((__bridge id)((__bridge CFArrayRef)([aStream propertyForKey:(NSString *) kCFStreamPropertySSLPeerCertificates])));
-            SecTrustCreateWithCertificates(CFBridgingRetain((__bridge id)(streamCertificatesRef)),
-                                           policy,
-                                           &trust);
-            
-            SecTrustResultType trustResultType = kSecTrustResultInvalid;
-            SecTrustEvaluate(trust, &trustResultType);
-            
-            BOOL proceed = NO;
-            switch (trustResultType) {
-                case kSecTrustResultProceed:
-                    proceed = YES;
-                    break;
-                case kSecTrustResultUnspecified:
-                    NSLog(@"Trusted by OS");
-                    proceed = YES;
-                    break;
-                case kSecTrustResultRecoverableTrustFailure:
-                    proceed = recoverFromTrustFailure(trust);
-                    break;
-                case kSecTrustResultDeny:
-                    NSLog(@"Deny");
-                    break;
-                case kSecTrustResultFatalTrustFailure:
-                    NSLog(@"FatalTrustFailure");
-                    break;
-                case kSecTrustResultOtherError:
-                    NSLog(@"OtherError");
-                    break;
-                case kSecTrustResultInvalid:
-                    NSLog(@"Invalid");
-                    break;
-                default:
-                    NSLog(@"Default");
-                    break;
-            }
-            
-            if (trust) {
-                CFRelease(trust);
-            }
-            if (policy) {
-                CFRelease(policy);
-            }
-            if (!proceed) {
-                NSLog(@"Cannot trust certificate. TrustResultType: %u", trustResultType);
-                [aStream close];
-                @throw [TSSLSocketException exceptionWithReason: @"Cannot trust certificate"];
-            }
-        }
-            break;
-        case NSStreamEventErrorOccurred:
-        {
-            NSError *theError = [aStream streamError];
-            NSLog(@"Error occurred opening stream: %@", theError);
-//            @throw [TSSLSocketException exceptionWithReason: @"Error occurred opening stream" error: theError];
-            break;
-        }
-        case NSStreamEventEndEncountered:
-            break;
-    }
-}
-
-bool recoverFromTrustFailure(SecTrustRef myTrust)
-{
-    
-    SecTrustResultType trustResult;
-    OSStatus status = SecTrustEvaluate(myTrust, &trustResult);
-    
-    CFAbsoluteTime trustTime,currentTime,timeIncrement,newTime;
-    CFDateRef newDate;
-    if (trustResult == kSecTrustResultRecoverableTrustFailure) {
-        trustTime = SecTrustGetVerifyTime(myTrust);
-        timeIncrement = 31536000;
-        currentTime = CFAbsoluteTimeGetCurrent();
-        newTime = currentTime - timeIncrement;
-        if (trustTime - newTime){
-            newDate = CFDateCreate(NULL, newTime);
-            SecTrustSetVerifyDate(myTrust, newDate);
-            status = SecTrustEvaluate(myTrust, &trustResult);
-        }
-    }
-    if (trustResult != kSecTrustResultProceed) {
-        NSLog(@"Certificate trust failure");
-        return false;
-    }
-    return true;
-}
-
-- (void)close
-{
-    if(self.mInput) {
-        //Close and reset inputstream
-        CFReadStreamSetProperty((__bridge CFReadStreamRef)(self.mInput), kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
-        [self.mInput setDelegate:nil];
-        [self.mInput close];
-        [self.mInput removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
-        self.mInput = nil;
-    }
-    
-    if(self.mOutput) {
-        //Close and reset outputstream
-        CFReadStreamSetProperty((__bridge CFReadStreamRef)(self.mOutput), kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
-        [self.mOutput setDelegate:nil];
-        [self.mOutput close];
-        [self.mOutput removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
-        self.mOutput = nil;
-    }
-}
-
-- (BOOL) isOpen
-{
-    if(sd > 0)
-        return TRUE;
-    else
-        return FALSE;
-}
-
-@end
-
diff --git a/lib/cocoa/src/transport/TSSLSocketException.h b/lib/cocoa/src/transport/TSSLSocketException.h
deleted file mode 100644
index c290b05..0000000
--- a/lib/cocoa/src/transport/TSSLSocketException.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#import "TTransportException.h"
-
-@interface TSSLSocketException : TTransportException
-
-+ (id) exceptionWithReason: (NSString *) reason
-                     error: (NSError *) error;
-
-+ (id) exceptionWithReason: (NSString *) reason;
-
-@end
diff --git a/lib/cocoa/src/transport/TSSLSocketException.m b/lib/cocoa/src/transport/TSSLSocketException.m
deleted file mode 100644
index 99eadf5..0000000
--- a/lib/cocoa/src/transport/TSSLSocketException.m
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#import "TSSLSocketException.h"
-
-@implementation TSSLSocketException
-
-+ (id) exceptionWithReason: (NSString *) reason
-                     error: (NSError *) error
-{
-    NSDictionary * userInfo = nil;
-    if (error != nil) {
-        userInfo = [NSDictionary dictionaryWithObject: error forKey: @"error"];
-    }
-    
-    return [super exceptionWithName: @"TSSLSocketException"
-                             reason: reason
-                           userInfo: userInfo];
-}
-
-+ (id) exceptionWithReason: (NSString *) reason
-{
-    return [self exceptionWithReason: reason error: nil];
-}
-
-@end
diff --git a/lib/cocoa/src/transport/TSocketClient.h b/lib/cocoa/src/transport/TSSLSocketTransport.h
similarity index 77%
copy from lib/cocoa/src/transport/TSocketClient.h
copy to lib/cocoa/src/transport/TSSLSocketTransport.h
index 81a0247..b606c4a 100644
--- a/lib/cocoa/src/transport/TSocketClient.h
+++ b/lib/cocoa/src/transport/TSSLSocketTransport.h
@@ -20,17 +20,18 @@
 #import <Foundation/Foundation.h>
 #import "TNSStreamTransport.h"
 
-@interface TSocketClient : TNSStreamTransport 
-#if TARGET_OS_IPHONE || (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
-<NSStreamDelegate>
-#endif
-{
-}
+NS_ASSUME_NONNULL_BEGIN
 
-- (id) initWithHostname: (NSString *) hostname
-                   port: (UInt32) port;
+
+@interface TSSLSocketTransport : TNSStreamTransport <NSStreamDelegate>
+
+-(id) initWithHostname:(NSString *)hostname
+                  port:(int)port
+                 error:(NSError **)error;
+
+-(BOOL) isOpen;
 
 @end
 
 
-
+NS_ASSUME_NONNULL_END
diff --git a/lib/cocoa/src/transport/TSSLSocketTransport.m b/lib/cocoa/src/transport/TSSLSocketTransport.m
new file mode 100644
index 0000000..ab5eb3d
--- /dev/null
+++ b/lib/cocoa/src/transport/TSSLSocketTransport.m
@@ -0,0 +1,304 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#import <Foundation/Foundation.h>
+#import <CoreFoundation/CoreFoundation.h>
+#import "TSSLSocketTransport.h"
+#import "TSSLSocketTransportError.h"
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+
+#if !TARGET_OS_IPHONE
+#import <CoreServices/CoreServices.h>
+#else
+#import <CFNetwork/CFNetwork.h>
+#endif
+
+@interface TSSLSocketTransport ()
+
+@property(strong, nonatomic) NSString *sslHostname;
+@property(assign, nonatomic) int sd;
+
+@end
+
+
+@implementation TSSLSocketTransport
+
+-(id) initWithHostname:(NSString *)hostname
+                  port:(int)port
+                 error:(NSError **)error
+{
+  _sslHostname = hostname;
+  CFReadStreamRef readStream = NULL;
+  CFWriteStreamRef writeStream = NULL;
+
+
+  /* create a socket structure */
+  struct sockaddr_in pin;
+  struct hostent *hp = NULL;
+  for (int i = 0; i < 10; i++) {
+
+
+
+    if ((hp = gethostbyname([hostname UTF8String])) == NULL) {
+      NSLog(@"failed to resolve hostname %@", hostname);
+      herror("resolv");
+      if (i == 9) {
+        if (error) {
+          *error = [NSError errorWithDomain:TSSLSocketTransportErrorDomain
+                                       code:TSSLSocketTransportErrorHostanameResolution
+                                   userInfo:nil];
+        }
+        return nil;
+      }
+      [NSThread sleepForTimeInterval:0.2];
+    }
+    else {
+      break;
+    }
+  }
+
+  memset(&pin, 0, sizeof(pin));
+  pin.sin_family = AF_INET;
+  memcpy(&pin.sin_addr, hp->h_addr, sizeof(struct in_addr));
+  pin.sin_port = htons(port);
+
+  /* create the socket */
+  if ((_sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
+    NSLog(@"failed to create socket for host %@:%d", hostname, port);
+    if (error) {
+      *error = [NSError errorWithDomain:TSSLSocketTransportErrorDomain
+                                   code:TSSLSocketTransportErrorSocketCreate
+                               userInfo:nil];
+    }
+    return nil;
+  }
+
+  /* open a connection */
+  if (connect(_sd, (struct sockaddr *)&pin, sizeof(pin)) == -1) {
+    NSLog(@"failed to create conenct to host %@:%d", hostname, port);
+    if (error) {
+      *error = [NSError errorWithDomain:TSSLSocketTransportErrorDomain
+                                   code:TSSLSocketTransportErrorConnect
+                               userInfo:nil];
+    }
+    return nil;
+  }
+  CFStreamCreatePairWithSocket(kCFAllocatorDefault, _sd, &readStream, &writeStream);
+
+  CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
+  CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
+
+  NSInputStream *inputStream;
+  NSOutputStream *outputStream;
+
+  if (readStream && writeStream) {
+
+    CFReadStreamSetProperty(readStream,
+                            kCFStreamPropertySocketSecurityLevel,
+                            kCFStreamSocketSecurityLevelTLSv1);
+
+    NSDictionary *settings = @{(__bridge NSString *)kCFStreamSSLValidatesCertificateChain: @YES};
+
+    CFReadStreamSetProperty((CFReadStreamRef)readStream,
+                            kCFStreamPropertySSLSettings,
+                            (CFTypeRef)settings);
+
+    CFWriteStreamSetProperty((CFWriteStreamRef)writeStream,
+                             kCFStreamPropertySSLSettings,
+                             (CFTypeRef)settings);
+
+    inputStream = (__bridge NSInputStream *)readStream;
+    [inputStream setDelegate:self];
+    [inputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
+    [inputStream open];
+
+    outputStream = (__bridge NSOutputStream *)writeStream;
+    [outputStream setDelegate:self];
+    [outputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
+    [outputStream open];
+
+    CFRelease(readStream);
+    CFRelease(writeStream);
+  }
+
+  self = [super initWithInputStream:inputStream outputStream:outputStream];
+
+  return self;
+}
+
+-(void) dealloc
+{
+  [self close];
+}
+
+#pragma mark -
+#pragma mark NSStreamDelegate
+
+-(void) stream:(NSStream *)aStream
+   handleEvent:(NSStreamEvent)eventCode
+{
+  switch (eventCode) {
+  case NSStreamEventNone:
+    break;
+
+  case NSStreamEventHasBytesAvailable:
+    break;
+
+  case NSStreamEventOpenCompleted:
+    break;
+
+  case NSStreamEventHasSpaceAvailable: {
+
+    BOOL proceed = NO;
+    SecTrustResultType trustResult = kSecTrustResultInvalid;
+    CFMutableArrayRef newPolicies = NULL;
+
+    do {
+
+      SecTrustRef trust = (__bridge SecTrustRef)[aStream propertyForKey:(NSString *)kCFStreamPropertySSLPeerTrust];
+
+      // Add new policy to current list of policies
+      SecPolicyRef policy = SecPolicyCreateSSL(NO, (__bridge CFStringRef)(_sslHostname));
+      if (!policy) {
+        break;
+      }
+
+      CFArrayRef policies;
+      if (SecTrustCopyPolicies(trust, &policies) != errSecSuccess) {
+        CFRelease(policy);
+        break;
+      }
+
+      newPolicies = CFArrayCreateMutableCopy(NULL, 0, policies);
+      CFArrayAppendValue(newPolicies, policy);
+
+      CFRelease(policies);
+      CFRelease(policy);
+
+      // Update trust policies
+      if (SecTrustSetPolicies(trust, newPolicies) != errSecSuccess) {
+        break;
+      }
+
+      // Evaluate the trust chain
+      if (SecTrustEvaluate(trust, &trustResult) != errSecSuccess) {
+        break;
+      }
+
+      switch (trustResult) {
+      case kSecTrustResultProceed:
+        // NSLog(@"Trusted by USER");
+        proceed = YES;
+        break;
+
+      case kSecTrustResultUnspecified:
+        // NSLog(@"Trusted by OS");
+        proceed = YES;
+        break;
+
+      case kSecTrustResultRecoverableTrustFailure:
+        proceed = recoverFromTrustFailure(trust, trustResult);
+        break;
+
+      case kSecTrustResultDeny:
+        // NSLog(@"Deny");
+        break;
+
+      case kSecTrustResultFatalTrustFailure:
+        // NSLog(@"FatalTrustFailure");
+        break;
+
+      case kSecTrustResultOtherError:
+        // NSLog(@"OtherError");
+        break;
+
+      case kSecTrustResultInvalid:
+        // NSLog(@"Invalid");
+        break;
+
+      default:
+        // NSLog(@"Default");
+        break;
+      }
+
+    }
+    while (NO);
+
+    if (!proceed) {
+      NSLog(@"TSSLSocketTransport: Cannot trust certificate. Result: %u", trustResult);
+      [aStream close];
+    }
+
+    if (newPolicies) {
+      CFRelease(newPolicies);
+    }
+
+  }
+  break;
+
+  case NSStreamEventErrorOccurred: {
+    NSLog(@"TSSLSocketTransport: Error occurred opening stream: %@", [aStream streamError]);
+    break;
+  }
+
+  case NSStreamEventEndEncountered:
+    break;
+  }
+}
+
+BOOL recoverFromTrustFailure(SecTrustRef myTrust, SecTrustResultType lastTrustResult)
+{
+  CFAbsoluteTime trustTime = SecTrustGetVerifyTime(myTrust);
+  CFAbsoluteTime currentTime = CFAbsoluteTimeGetCurrent();
+
+  CFAbsoluteTime timeIncrement = 31536000;
+  CFAbsoluteTime newTime = currentTime - timeIncrement;
+
+  if (trustTime - newTime) {
+
+    CFDateRef newDate = CFDateCreate(NULL, newTime);
+    SecTrustSetVerifyDate(myTrust, newDate);
+    CFRelease(newDate);
+
+    if (SecTrustEvaluate(myTrust, &lastTrustResult) != errSecSuccess) {
+      return NO;
+    }
+
+  }
+
+  if (lastTrustResult == kSecTrustResultProceed || lastTrustResult == kSecTrustResultUnspecified) {
+    return YES;
+  }
+
+  NSLog(@"TSSLSocketTransport: Unable to recover certificate trust failure");
+  return YES;
+}
+
+-(BOOL) isOpen
+{
+  if (_sd > 0) {
+    return TRUE;
+  }
+  else {
+    return FALSE;
+  }
+}
+
+@end
diff --git a/lib/cocoa/src/transport/TTransportException.h b/lib/cocoa/src/transport/TSSLSocketTransportError.h
similarity index 72%
rename from lib/cocoa/src/transport/TTransportException.h
rename to lib/cocoa/src/transport/TSSLSocketTransportError.h
index 6749fe2..e17f39e 100644
--- a/lib/cocoa/src/transport/TTransportException.h
+++ b/lib/cocoa/src/transport/TSSLSocketTransportError.h
@@ -17,14 +17,14 @@
  * under the License.
  */
 
-#import "TException.h"
+#import "TTransportError.h"
 
-@interface TTransportException : TException {
-}
 
-+ (id) exceptionWithReason: (NSString *) reason
-                     error: (NSError *) error;
+extern NSString *TSSLSocketTransportErrorDomain;
 
-+ (id) exceptionWithReason: (NSString *) reason;
 
-@end
+typedef NS_ENUM (int, TSSLSocketTransportError) {
+  TSSLSocketTransportErrorHostanameResolution  = -10000,
+  TSSLSocketTransportErrorSocketCreate         = -10001,
+  TSSLSocketTransportErrorConnect              = -10002,
+};
diff --git a/lib/cocoa/src/protocol/TProtocolException.m b/lib/cocoa/src/transport/TSSLSocketTransportError.m
similarity index 86%
copy from lib/cocoa/src/protocol/TProtocolException.m
copy to lib/cocoa/src/transport/TSSLSocketTransportError.m
index 681487a..bcf941c 100644
--- a/lib/cocoa/src/protocol/TProtocolException.m
+++ b/lib/cocoa/src/transport/TSSLSocketTransportError.m
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-#import "TProtocolException.h"
+#import "TSSLSocketTransportError.h"
 
-@implementation TProtocolException
-@end
+
+NSString *TSSLSocketTransportErrorDomain = @"TSSLSocketTransportErrorDomain";
diff --git a/lib/cocoa/src/transport/TSocketClient.m b/lib/cocoa/src/transport/TSocketClient.m
deleted file mode 100644
index b0bac74..0000000
--- a/lib/cocoa/src/transport/TSocketClient.m
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-#import "TSocketClient.h"
-#import "TObjective-C.h"
-
-#if !TARGET_OS_IPHONE
-#import <CoreServices/CoreServices.h>
-#else
-#import <CFNetwork/CFNetwork.h>
-#endif
-
-@interface TSocketClient ()
-{
-    NSInputStream * inputStream;
-	NSOutputStream * outputStream;
-}
-@end
-
-@implementation TSocketClient
-
-- (id) initWithHostname: (NSString *) hostname
-                   port: (UInt32) port
-{
-	inputStream = NULL;
-	outputStream = NULL;
-	CFReadStreamRef readStream = NULL;
-	CFWriteStreamRef writeStream = NULL;
-	CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (bridge_stub CFStringRef)hostname, port, &readStream, &writeStream);
-	if (readStream && writeStream) {
-		CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
-		CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
-		
-		inputStream = (bridge_stub NSInputStream *)readStream;
-		[inputStream retain_stub];
-		[inputStream setDelegate:self];
-		[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
-		[inputStream open];
-		
-		outputStream = (bridge_stub NSOutputStream *)writeStream;
-		[outputStream retain_stub];
-		[outputStream setDelegate:self];
-		[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
-		[outputStream open];
-        CFRelease(readStream);
-        CFRelease(writeStream);
-	}
-	
-	self = [super initWithInputStream: inputStream outputStream: outputStream];
-	
-	return self;
-}
-
--(void)dealloc
-{
-    [inputStream close];
-    [inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
-    [inputStream setDelegate:nil];
-    [inputStream release_stub];
-    
-    [outputStream close];
-    [outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
-    [outputStream setDelegate:nil];
-    [outputStream release_stub];
-    [super dealloc_stub];
-}
-
-
-@end
-
-
-
diff --git a/lib/cocoa/src/transport/TSocketClient.h b/lib/cocoa/src/transport/TSocketTransport.h
similarity index 78%
rename from lib/cocoa/src/transport/TSocketClient.h
rename to lib/cocoa/src/transport/TSocketTransport.h
index 81a0247..4ea03cc 100644
--- a/lib/cocoa/src/transport/TSocketClient.h
+++ b/lib/cocoa/src/transport/TSocketTransport.h
@@ -20,17 +20,15 @@
 #import <Foundation/Foundation.h>
 #import "TNSStreamTransport.h"
 
-@interface TSocketClient : TNSStreamTransport 
-#if TARGET_OS_IPHONE || (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
-<NSStreamDelegate>
-#endif
-{
-}
+NS_ASSUME_NONNULL_BEGIN
 
-- (id) initWithHostname: (NSString *) hostname
-                   port: (UInt32) port;
+
+@interface TSocketTransport : TNSStreamTransport
+
+-(id) initWithHostname:(NSString *)hostname
+                  port:(int)port;
 
 @end
 
 
-
+NS_ASSUME_NONNULL_END
diff --git a/lib/cocoa/src/transport/TSocketTransport.m b/lib/cocoa/src/transport/TSocketTransport.m
new file mode 100644
index 0000000..1b8fe9b
--- /dev/null
+++ b/lib/cocoa/src/transport/TSocketTransport.m
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#import "TSocketTransport.h"
+
+#if !TARGET_OS_IPHONE
+#import <CoreServices/CoreServices.h>
+#else
+#import <CFNetwork/CFNetwork.h>
+#endif
+
+@interface TSocketTransport () <NSStreamDelegate>
+@end
+
+
+@implementation TSocketTransport
+
+-(id) initWithHostname:(NSString *)hostname
+                  port:(int)port
+{
+  NSInputStream *inputStream = nil;
+  NSOutputStream *outputStream = nil;
+
+  CFReadStreamRef readStream = NULL;
+  CFWriteStreamRef writeStream = NULL;
+  CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef)hostname, port, &readStream, &writeStream);
+  if (readStream && writeStream) {
+
+    CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
+    CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
+
+    inputStream = (__bridge NSInputStream *)readStream;
+    [inputStream setDelegate:self];
+    [inputStream scheduleInRunLoop:NSRunLoop.mainRunLoop forMode:NSDefaultRunLoopMode];
+    [inputStream open];
+
+    outputStream = (__bridge NSOutputStream *)writeStream;
+    [outputStream setDelegate:self];
+    [outputStream scheduleInRunLoop:NSRunLoop.mainRunLoop forMode:NSDefaultRunLoopMode];
+    [outputStream open];
+  }
+  else {
+
+    if (readStream) {
+      CFRelease(readStream);
+    }
+
+    if (writeStream) {
+      CFRelease(writeStream);
+    }
+
+    return nil;
+  }
+
+  return [super initWithInputStream:inputStream outputStream:outputStream];
+}
+
+@end
diff --git a/lib/cocoa/src/transport/TTransport.h b/lib/cocoa/src/transport/TTransport.h
index 83aad9e..558cf60 100644
--- a/lib/cocoa/src/transport/TTransport.h
+++ b/lib/cocoa/src/transport/TTransport.h
@@ -17,20 +17,32 @@
  * under the License.
  */
 
+#import <Foundation/Foundation.h>
+
+
+NS_ASSUME_NONNULL_BEGIN
+
+
 @protocol TTransport <NSObject>
 
-  /**
-   * Guarantees that all of len bytes are read
-   *
-   * @param buf Buffer to read into
-   * @param offset Index in buffer to start storing bytes at
-   * @param length Maximum number of bytes to read
-   * @return The number of bytes actually read, which must be equal to len
-   * @throws TTransportException if there was an error reading data
-   */
-- (size_t) readAll: (uint8_t *) buf offset: (size_t) offset length: (size_t) length;
+/**
+ * Guarantees that all of len bytes are read
+ *
+ * @param buf Buffer to read into
+ * @param off Index in buffer to start storing bytes at
+ * @param len Maximum number of bytes to read
+ * @return YES if succeeded, NO if failed
+ * @throws TTransportError if there was an error reading data
+ */
+-(BOOL) readAll:(UInt8 *)buf offset:(UInt32)off length:(UInt32)len error:(NSError *__autoreleasing *)error;
 
-- (void) write: (const uint8_t *) data offset: (size_t) offset length: (size_t) length;
+-(UInt32) readAvail:(UInt8 *)buf offset:(UInt32)off maxLength:(UInt32)maxLen error:(NSError *__autoreleasing *)error;
 
-- (void) flush;
+-(BOOL) write:(const UInt8 *)data offset:(UInt32)offset length:(UInt32)length error:(NSError *__autoreleasing *)error;
+
+-(BOOL) flush:(NSError *__autoreleasing *)error;
+
 @end
+
+
+NS_ASSUME_NONNULL_END
diff --git a/lib/cocoa/src/transport/TTransportError.h b/lib/cocoa/src/transport/TTransportError.h
new file mode 100644
index 0000000..c8c9f06
--- /dev/null
+++ b/lib/cocoa/src/transport/TTransportError.h
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#import "TError.h"
+
+
+extern NSString *TTransportErrorDomain;
+
+
+typedef NS_ENUM (int, TTransportError) {
+  TTransportErrorUnknown        = 0,
+  TTransportErrorNotOpen        = 1,
+  TTransportErrorAlreadyOpen    = 2,
+  TTransportErrorTimedOut       = 3,
+  TTransportErrorEndOfFile      = 4,
+};
+
+
+extern NSString *TTransportErrorExtendedErrorKey;
+extern NSString *TTransportErrorHttpErrorKey;
+
+
+typedef NS_ENUM(int, THttpTransportError) {
+  THttpTransportErrorInvalidResponse  = 1001,
+  THttpTransportErrorInvalidStatus    = 1002,
+  THttpTransportErrorAuthentication   = 1003,
+};
diff --git a/lib/cocoa/src/protocol/TProtocolException.h b/lib/cocoa/src/transport/TTransportError.m
similarity index 79%
copy from lib/cocoa/src/protocol/TProtocolException.h
copy to lib/cocoa/src/transport/TTransportError.m
index ad354fc..b1af076 100644
--- a/lib/cocoa/src/protocol/TProtocolException.h
+++ b/lib/cocoa/src/transport/TTransportError.m
@@ -17,9 +17,11 @@
  * under the License.
  */
 
-#import "TException.h"
+#import "TTransportError.h"
 
-@interface TProtocolException : TException {
-}
 
-@end
+NSString *TTransportErrorDomain = @"TTransportErrorDomain";
+
+
+NSString *TTransportErrorExtendedErrorKey = @"extendedError";
+NSString *TTransportErrorHttpErrorKey = @"httpError";
diff --git a/lib/cocoa/src/transport/TTransportException.m b/lib/cocoa/src/transport/TTransportException.m
deleted file mode 100644
index 43cdfbd..0000000
--- a/lib/cocoa/src/transport/TTransportException.m
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#import "TTransportException.h"
-#import "TObjective-C.h"
-
-@implementation TTransportException
-
-+ (id) exceptionWithReason: (NSString *) reason
-                     error: (NSError *) error
-{
-  NSDictionary * userInfo = nil;
-  if (error != nil) {
-    userInfo = [NSDictionary dictionaryWithObject: error forKey: @"error"];
-  }
-
-  return [super exceptionWithName: @"TTransportException"
-                           reason: reason
-                         userInfo: userInfo];
-}
-
-
-+ (id) exceptionWithReason: (NSString *) reason
-{
-  return [self exceptionWithReason: reason error: nil];
-}
-
-@end