     func updateAdapter(newAdapter: AdapterSocket) {
         adapterSocket = newAdapter
         adapterSocket?.delegate = self
-        adapterSocket?.queue = delegateQueue
+        adapterSocket?.queue = queue
     }
 
     private func checkStatus() {
-        if closed {
+        if isClosed {
             delegate?.tunnelDidClose(self)
             delegate = nil
         }

    func tunnelDidClose(tunnel: Tunnel)
}

class Tunnel: NSObject, SocketDelegate {
    var proxySocket: ProxySocket
    var adapterSocket: AdapterSocket?

    weak var delegate: TunnelDelegate?

    var delegateQueue = dispatch_queue_create("TunnelQueue", DISPATCH_QUEUE_SERIAL) {
        didSet {
            self.proxySocket.queue = delegateQueue
            self.adapterSocket?.queue = delegateQueue
        }
    }

    var readySignal = 0

    var closed: Bool {
        return proxySocket.isDisconnected && (adapterSocket?.isDisconnected ?? true)
    }

    init(proxySocket: ProxySocket) {
        self.proxySocket = proxySocket
        self.proxySocket.queue = delegateQueue
        super.init()
        self.proxySocket.delegate = self
    }

    func openTunnel() {
        proxySocket.openSocket()
    }

    func close() {
        if !proxySocket.isDisconnected {
            proxySocket.disconnect()

        let manager = RuleManager.currentManager
        let factory = manager.match(request)
        adapterSocket = factory.getAdapter(request)
        adapterSocket!.queue = queue
        adapterSocket!.delegate = self
        adapterSocket!.openSocketWithRequest(request)
    }
