         }
     }
 
-    private func processStream(stream: Stream) throws {
+    private func processStream(_ stream: Stream) throws {
         while !stream.closed {
             do {
-                let data = try stream.receive(upTo: 2048)
+                let data = try stream.receive(upTo: bufferSize)
                 try processData(data, stream: stream)
-            } catch StreamError.closedStream {
-                break
             } catch {
                 let response = Response(status: .internalServerError)
                 try serializer.serialize(response, to: stream)

        }
    }

    private func processData(data: Data, stream: Stream) throws {
        if let request = try parser.parse(data) {
            let response = try middleware.chain(to: responder).respond(to: request)
            try serializer.serialize(response, to: stream)

            if let upgrade = response.upgrade {
                try upgrade(request, stream)
                stream.close()
            }

            if !request.isKeepAlive {
                stream.close()
                throw StreamError.closedStream(data: [])
            }
        }
    }

    public func startInBackground(failure: ErrorProtocol -> Void = Server.printError) {
        co {
            do {
                try self.start()

        }
    }

    private static func printError(_ error: ErrorProtocol) -> Void {
        print("Error: \(error)")
    }

