title: Implementing handshake for hybi-17
body: <p>I'm trying to develop the <strong>handshake</strong> for <strong>websocket hybi-17 protocol</strong> (<a href="https://datatracker.ietf.org/doc/html/draft-ietf-hybi-thewebsocketprotocol-17" rel="nofollow noreferrer">https://datatracker.ietf.org/doc/html/draft-ietf-hybi-thewebsocketprotocol-17</a>).
  According to that draft, I made the following code for the client (user-agent):</p>
  <pre><code>var host = 'ws://localhost/server.php';
  if ('MozWebSocket' in window) ws = new MozWebSocket (host);
  else ws = new WebSocket (host);
  </code></pre>
  <p>and this code for the server (I skipped the socket initialization/management part):</p>
  <pre><code>$key = $value = null;
  preg_match (&quot;#Sec-WebSocket-Key: (.*?)\r\n#&quot;, $buffer, $match) &amp;&amp; $key = $match[1];
  $key .= &quot;258EAFA5-E914-47DA-95CA-C5AB0DC85B11&quot;;
  $key = sha1 ($key);
  $key = pack ('H*', $key);
  $key = base64_encode ($key);
  
  $value = 
    &quot;HTTP/1.1 101 Switching Protocols\r\n&quot; .
    &quot;Upgrade: websocket\r\n&quot; .
    &quot;Connection: Upgrade\r\n&quot; .
    &quot;Sec-WebSocket-Accept: {$key}&quot;;
  
  socket_write ($socket, $value, strlen ($value));
  </code></pre>
  <p>Now, following an example, starting with the client request (simply done with 'new MozWebSocket (host)' call):</p>
  <pre><code>GET /server.php HTTP/1.1
  Host: localhost
  User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3
  Accept-Encoding: gzip, deflate
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  Connection: keep-alive, Upgrade
  Sec-WebSocket-Version: 8
  Sec-WebSocket-Origin: http://localhost
  Sec-WebSocket-Extensions: deflate-stream
  Sec-WebSocket-Key: oqFCBULD7k+BM41Bc3VEeA==
  Pragma: no-cache
  Cache-Control: no-cache
  Upgrade: websocket
  </code></pre>
  <p>The server response (echoed in the local shell, as a debug line):</p>
  <pre><code>HTTP/1.1 101 Switching Protocols
  Upgrade: websocket
  Connection: Upgrade
  Sec-WebSocket-Accept: TlKc0Ck7WpqsLhMm/QXABMQWARk=
  </code></pre>
  <p>I followed what specified in the IETF hybi-17 draft but the client request <em>is still pending</em> and there's no real connection between client and server.</p>
  <p>What's wrong?
  What I have to do more?</p>
  <p>Thanks in advance.</p>