diff --git a/src/websockets/qwebsocket_p.cpp b/src/websockets/qwebsocket_p.cpp
index f4f7ea4..16afc5b 100644
--- a/src/websockets/qwebsocket_p.cpp
+++ b/src/websockets/qwebsocket_p.cpp
@@ -137,7 +137,8 @@ QWebSocketPrivate::QWebSocketPrivate(QTcpSocket *pTcpSocket, QWebSocketProtocol:
     m_dataProcessor(),
     m_configuration(),
     m_pMaskGenerator(&m_defaultMaskGenerator),
-    m_defaultMaskGenerator()
+    m_defaultMaskGenerator(),
+    m_handshakeState(NothingDoneState)
 {
 }
 
@@ -889,45 +890,58 @@ void QWebSocketPrivate::processHandshake(QTcpSocket *pSocket)
     if (Q_UNLIKELY(!pSocket))
         return;
 
-    bool ok = false;
     QString errorDescription;
 
-    const QByteArray statusLine = pSocket->readLine();
-    int httpMajorVersion, httpMinorVersion;
-    int httpStatusCode;
-    QString httpStatusMessage;
-    if (Q_UNLIKELY(!parseStatusLine(statusLine, &httpMajorVersion, &httpMinorVersion,
-                                    &httpStatusCode, &httpStatusMessage))) {
-        errorDescription = QWebSocket::tr("Invalid statusline in response: %1.").arg(QString::fromLatin1(statusLine));
-    } else {
-        QString headerLine = readLine(pSocket);
-        QMap<QString, QString> headers;
-        while (!headerLine.isEmpty()) {
+    switch (m_handshakeState) {
+    case NothingDoneState:
+        m_handshakeState = ReadingStatusState;
+    case ReadingStatusState: {
+        if (!pSocket->canReadLine())
+            return;
+        m_statusLine = pSocket->readLine();
+        if (Q_UNLIKELY(!parseStatusLine(m_statusLine, &m_httpMajorVersion, &m_httpMinorVersion, &m_httpStatusCode, &m_httpStatusMessage))) {
+            errorDescription = QWebSocket::tr("Invalid statusline in response: %1.").arg(QString::fromLatin1(m_statusLine));
+            break;
+        }
+        m_handshakeState = ReadingHeaderState;
+    }
+    case ReadingHeaderState: {
+        while (pSocket->canReadLine()) {
+            QString headerLine = readLine(pSocket);
             const QStringList headerField = headerLine.split(QStringLiteral(": "),
                                                              QString::SkipEmptyParts);
             if (headerField.size() == 2) {
-                headers.insertMulti(headerField[0].toLower(), headerField[1]);
+                m_headers.insertMulti(headerField[0].toLower(), headerField[1]);
             }
-            headerLine = readLine(pSocket);
+            if (headerField.isEmpty()) {
+                m_handshakeState = AllDoneState;
+                break;
+            }
+        }
+        if (pSocket->atEnd()) {
+            errorDescription = QWebSocket::tr("QWebSocketPrivate::processHandshake: Connection closed while reading header.");
+            break;
         }
 
-        const QString acceptKey = headers.value(QStringLiteral("sec-websocket-accept"),
-                                                QString());
-        const QString upgrade = headers.value(QStringLiteral("upgrade"), QString());
-        const QString connection = headers.value(QStringLiteral("connection"), QString());
+        if (m_handshakeState != AllDoneState)
+            return;
+
+        const QString acceptKey = m_headers.value(QStringLiteral("sec-websocket-accept"), QString());
+        const QString upgrade = m_headers.value(QStringLiteral("upgrade"), QString());
+        const QString connection = m_headers.value(QStringLiteral("connection"), QString());
 //        unused for the moment
-//        const QString extensions = headers.value(QStringLiteral("sec-websocket-extensions"),
+//        const QString extensions = m_headers.value(QStringLiteral("sec-websocket-extensions"),
 //                                                 QString());
-//        const QString protocol = headers.value(QStringLiteral("sec-websocket-protocol"),
+//        const QString protocol = m_headers.value(QStringLiteral("sec-websocket-protocol"),
 //                                               QString());
-        const QString version = headers.value(QStringLiteral("sec-websocket-version"),
-                                              QString());
+        const QString version = m_headers.value(QStringLiteral("sec-websocket-version"), QString());
 
-        if (Q_LIKELY(httpStatusCode == 101)) {
+        bool ok = false;
+        if (Q_LIKELY(m_httpStatusCode == 101)) {
             //HTTP/x.y 101 Switching Protocols
             //TODO: do not check the httpStatusText right now
             ok = !(acceptKey.isEmpty() ||
-                   (httpMajorVersion < 1 || httpMinorVersion < 1) ||
+                   (m_httpMajorVersion < 1 || m_httpMinorVersion < 1) ||
                    (upgrade.toLower() != QStringLiteral("websocket")) ||
                    (connection.toLower() != QStringLiteral("upgrade")));
             if (ok) {
@@ -940,9 +954,9 @@ void QWebSocketPrivate::processHandshake(QTcpSocket *pSocket)
             } else {
                 errorDescription =
                     QWebSocket::tr("QWebSocketPrivate::processHandshake: Invalid statusline in response: %1.")
-                        .arg(QString::fromLatin1(statusLine));
+                        .arg(QString::fromLatin1(m_statusLine));
             }
-        } else if (httpStatusCode == 400) {
+        } else if (m_httpStatusCode == 400) {
             //HTTP/1.1 400 Bad Request
             if (!version.isEmpty()) {
                 const QStringList versions = version.split(QStringLiteral(", "),
@@ -953,30 +967,40 @@ void QWebSocketPrivate::processHandshake(QTcpSocket *pSocket)
                     errorDescription =
                             QWebSocket::tr("Handshake: Server requests a version that we don't support: %1.")
                             .arg(versions.join(QStringLiteral(", ")));
-                    ok = false;
                 } else {
                     //we tried v13, but something different went wrong
                     errorDescription =
                         QWebSocket::tr("QWebSocketPrivate::processHandshake: Unknown error condition encountered. Aborting connection.");
-                    ok = false;
                 }
+            } else {
+                    errorDescription =
+                        QWebSocket::tr("QWebSocketPrivate::processHandshake: Unknown error condition encountered. Aborting connection.");
             }
         } else {
             errorDescription =
                     QWebSocket::tr("QWebSocketPrivate::processHandshake: Unhandled http status code: %1 (%2).")
-                        .arg(httpStatusCode).arg(httpStatusMessage);
+                        .arg(m_httpStatusCode).arg(m_httpStatusMessage);
             ok = false;
         }
+        // handshake failed
+        if (!ok)
+            m_handshakeState = NothingDoneState;
+        break;
+    }
+    case AllDoneState:
+        Q_UNREACHABLE();
+        break;
+    }
 
-        if (!ok) {
-            setErrorString(errorDescription);
-            Q_EMIT q->error(QAbstractSocket::ConnectionRefusedError);
-        } else {
-            //handshake succeeded
-            setSocketState(QAbstractSocket::ConnectedState);
-            Q_EMIT q->connected();
-        }
+    if (m_handshakeState == AllDoneState) {
+        //handshake succeeded
+        setSocketState(QAbstractSocket::ConnectedState);
+        Q_EMIT q->connected();
+    } else {
+        setErrorString(errorDescription);
+        Q_EMIT q->error(QAbstractSocket::ConnectionRefusedError);
     }
+    m_handshakeState = NothingDoneState;
 }
 
 /*!
diff --git a/src/websockets/qwebsocket_p.h b/src/websockets/qwebsocket_p.h
index f13e08d..cbad2a0 100644
--- a/src/websockets/qwebsocket_p.h
+++ b/src/websockets/qwebsocket_p.h
@@ -224,6 +224,18 @@ private:
     QMaskGenerator *m_pMaskGenerator;
     QDefaultMaskGenerator m_defaultMaskGenerator;
 
+    enum HandshakeState {
+        NothingDoneState,
+        ReadingStatusState,
+        ReadingHeaderState,
+        AllDoneState
+    } m_handshakeState;
+    QByteArray m_statusLine;
+    int m_httpStatusCode;
+    int m_httpMajorVersion, m_httpMinorVersion;
+    QString m_httpStatusMessage;
+    QMap<QString, QString> m_headers;
+
     friend class QWebSocketServerPrivate;
 };
 
