             while keepProcessing {
                 buffer.length = 0
                 let count = try socket.read(into: buffer)
+
                 if count != 0 {
-                    
+                    let parserStatus = response.parse(buffer)
+
+                    if parserStatus.state == .messageComplete {
+                        keepProcessing = false
+                        if parserStatus.bytesLeft != 0 {
+                            unparsedData = NSData(bytes: buffer.bytes+buffer.length-parserStatus.bytesLeft, length: parserStatus.bytesLeft)
+                        }
+                    }
                 }
                 else {
-                    
+                    keepProcessing = false
+                    errorFlag = true
+                    XCTFail("Server closed socket prematurely")
                 }
             }
         }
         catch let error {
-            response = nil
+            errorFlag = true
             XCTFail("Failed to send upgrade request. Error=\(error)")
         }
-        return response
+        return (errorFlag ? nil : response, unparsedData)
     }
     
     class TestServerDelegate : ServerDelegate {

    }
    
    func testNoRegistrations() {
        //performServerTest(TestServerDelegate()) { expectation in
        //    guard let socket = self.sendUpgradeRequest() else { return }
        //
        //    guard let response = self.processUpgradeResponse(socket: socket) else { return }
        //
        //}
    }
    
    private func sendUpgradeRequest() -> Socket? {

    
    static var allTests : [(String, (UpgradeTests) -> () throws -> Void)] {
        return [
            ("testNoRegistrations", testNoRegistrations),
            ("testWrongRegistration", testWrongRegistration)
        ]
    }
    
