     }
 
     private func readCallback(data: NSData?, tag: Int) {
-        delegateCall {
+        queueCall {
             guard let data = self.consumeReadData(data) else {
                 // remote read is closed, but this is okay, nothing need to be done, if this socket is read again, then error occurs.
                 return

    var scannerTag: Int!
    var readDataPrefix: NSData?

    var isConnected: Bool {
        return connection.state == .Connected
    }

    var sourceIPAddress: IPv4Address? {
        return nil
    }

    var sourcePort: Port? {
        return nil
    }

    var destinationIPAddress: IPv4Address? {
        return nil
    }

    var destinationPort: Port? {
        return nil
    }


    var cancelledSignaled = false

    // MARK: SocketProtocol implemention
    func connectTo(host: String, port: Int, enableTLS: Bool, tlsSettings: [NSObject : AnyObject]?) {
        let endpoint = NWHostEndpoint(hostname: host, port: "\(port)")
        let tlsParameters = NWTLSParameters()
        if let tlsSettings = tlsSettings as? [String: AnyObject] {

import NetworkExtension
import CocoaLumberjackSwift

/// The TCP socket build upon `NWTCPConnection`.
///
/// - warning: This class is not thread-safe, it is expected that the instance is accessed on the `queue` only.
class NWTCPSocket: NSObject, RawTCPSocketProtocol {
    static let ScannerReadTag = 10000
    private var connection: NWTCPConnection!

    var writePending = false
    var closeAfterWriting = false
