         if writeQueue != nil {
             writeQueue!.waitUntilAllOperationsAreFinished()
         }
-        if inputStream != nil {
-            inputStream!.removeFromRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
-            inputStream!.close()
-        }
-        if outputStream != nil {
-            outputStream!.removeFromRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
-            outputStream!.close()
+        if let stream = inputStream {
+            stream.removeFromRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
+            stream.close()
+        }
+        if let stream = outputStream {
+            stream.removeFromRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
+            stream.close()
         }        
         outputStream = nil
         isRunLoop = false

    private var outputStream: NSOutputStream?
    private var isRunLoop = false
    private var connected = false
    private var writeQueue: NSOperationQueue?
    private var readStack = Array<WSResponse>()
    private var inputQueue = Array<NSData>()


    ///Connect to the websocket server on a background thread
    public func connect() {
        if isCreated {
            return
        }
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), {
            self.isCreated = true
            self.createHTTPRequest()
            self.isCreated = false
        })
    }
    
