         
         ws?.disconnect()
         stopPolling()
+        client?.engineDidClose(reason)
     }
     
     /// Starts the connection to the server

    }
    
    public func disconnect(reason: String) {
        guard connected else { return closeOutEngine() }
        
        DefaultSocketLogger.Logger.log("Engine is being closed.", type: logType)
        
        if closed {
            closeOutEngine()
            client?.engineDidClose(reason)
            return
        }
        
        if websocket {
            sendWebSocketMessage("", withType: .Close, withData: [])
            closeOutEngine()
        } else {
            disconnectPolling()
        }
    }
    
    // We need to take special care when we're polling that we send it ASAP
    // Also make sure we're on the emitQueue since we're touching postWait
    private func disconnectPolling() {
        dispatch_sync(emitQueue) {
            self.postWait.append(String(SocketEnginePacketType.Close.rawValue))
            let req = self.createRequestForPostWithPostWait()
            self.doRequest(req) {_, _, _ in }
            self.closeOutEngine()
        }
    }


        }
    }
    
    private func closeOutEngine(reason: String) {
        sid = ""
        closed = true
        invalidated = true
