         socket!.writeData(NSData(bytes: data, length: data.count), withTimeout: -1, tag: tag)
     }
 
+    func sendConnectFrame() {
+        let frame = CocoaMQTTFrameConnect(client: self)
+        send(frame)
+        reader!.start()
+        delegate?.mqtt(self, didConnect: host, port: Int(port))
+    }
+
     //AsyncSocket Delegate
 
     public func socket(sock: GCDAsyncSocket!, didConnectToHost host: String!, port: UInt16) {

                sock.startTLS([kCFStreamSSLPeerName: self.host])
            #endif
        } else {
            let frame = CocoaMQTTFrameConnect(client: self)
            send(frame)
            reader!.start()
        }
        
        delegate?.mqtt(self, didConnect: host, port: Int(port))
    }
    
    public func socket(sock: GCDAsyncSocket!, didReceiveTrust trust: SecTrust!, completionHandler: ((Bool) -> Void)!) {

        #if DEBUG
            NSLog("CocoaMQTT: socketDidSecure")
        #endif
        sendConnectFrame()
    }

    public func socket(sock: GCDAsyncSocket!, didWriteDataWithTag tag: Int) {
