         }
     }
     //Add a header to the CFHTTPMessage by using the NSString bridges to CFString
-    private func addHeader(urlRequest: CFHTTPMessage,key: String, val: String) {
-        let nsKey: NSString = key
-        let nsVal: NSString = val
-        CFHTTPMessageSetHeaderFieldValue(urlRequest,
-            nsKey,
-            nsVal)
+    private func addHeader(urlRequest: CFHTTPMessage, key: NSString, val: NSString) {
+        CFHTTPMessageSetHeaderFieldValue(urlRequest, key, val)
     }
     //generate a websocket key as needed in rfc
     private func generateWebSocketKey() -> String {
         var key = ""
         let seed = 16
-        for (var i = 0; i < seed; i++) {
+        for _ in 0..<seed {
             let uni = UnicodeScalar(UInt32(97 + arc4random_uniform(25)))
             key += "\(Character(uni))"
         }

            }
        }
        if eventCode == .HasBytesAvailable {
            if(aStream == inputStream) {
                processInputStream()
            }
        } else if eventCode == .ErrorOccurred {

        let buf = NSMutableData(capacity: BUFFER_MAX)
        let buffer = UnsafeMutablePointer<UInt8>(buf!.bytes)
        let length = inputStream!.read(buffer, maxLength: BUFFER_MAX)
        
        guard length > 0 else { return }
        
        if !connected {
            connected = processHTTP(buffer, bufferLen: length)
            if !connected {
                let response = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, false).takeRetainedValue()
                CFHTTPMessageAppendBytes(response, buffer, length)
                let code = CFHTTPMessageGetResponseStatusCode(response)
                doDisconnect(errorWithDetail("Invalid HTTP upgrade", code: UInt16(code)))
            }
        } else {
            var process = false
            if inputQueue.count == 0 {
                process = true
            }
            inputQueue.append(NSData(bytes: buffer, length: length))
            if process {
                dequeueInput()
            }
        }
    }
    ///dequeue the incoming input so it is processed in order
    private func dequeueInput() {
        guard !inputQueue.isEmpty else { return }
        
        let data = inputQueue[0]
        var work = data
        if let fragBuffer = fragBuffer {
            let combine = NSMutableData(data: fragBuffer)
            combine.appendData(data)
            work = combine
            self.fragBuffer = nil
        }
        let buffer = UnsafePointer<UInt8>(work.bytes)
        processRawMessage(buffer, bufferLen: work.length)
        inputQueue = inputQueue.filter{$0 != data}
        dequeueInput()
    }
    ///Finds the HTTP Packet in the TCP stream, by looking for the CRLF.
    private func processHTTP(buffer: UnsafePointer<UInt8>, bufferLen: Int) -> Bool {
        let CRLFBytes = [UInt8(ascii: "\r"), UInt8(ascii: "\n"), UInt8(ascii: "\r"), UInt8(ascii: "\n")]
        var k = 0
        var totalSize = 0
        for i in 0..<bufferLen {
            if buffer[i] == CRLFBytes[k] {
                k++
                if k == 3 {
