title: Make a PHP socket server for WebSocket [handshake]
body: <p>I try to make my own PHP socket Server in order to work with HTML5 WebSocket API, but i can not do the handshake step, i read the [rfc6455][1] here my PHP Server code :</p>
  <p>Here the Javascript code :</p>
  <pre><code>var socket = new WebSocket(&quot;ws://127.0.0.1:1577/server.php&quot;);
  
  socket.onopen = function (e) {
      console.log(&quot;openned : &quot;+e.readyState);
  }
  socket.onclose = function (e) {
      console.log(&quot;Socket connection closed : &quot;+e);
  }
  </code></pre>
  <p>Here the PHP code :</p>
  <pre><code>&lt;?php
  
  set_time_limit(0);
  $adr = &quot;127.0.0.1&quot;;
  $port = 1577;
   
  $m_sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  $msg = &quot;Welcome...&quot;;
  $cls = array();
   
  socket_bind($m_sock, $adr, $port);
  socket_listen($m_sock, 5);
  echo &quot;Server start...\n\n&quot;;
  
  do{
          $msgsock = socket_accept($m_sock);
   
          array_push($cls, $msgsock);
          echo &quot;Connected...\n\n&quot;;
          usleep(100);
          //socket_write($msgsock, $msg, strlen($msg)); //this is the 'bug'
          do{
                  if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
                     echo &quot;socket_read() fail :&quot; . socket_strerror(socket_last_error($msgsock)) . &quot;\n&quot;;
                      break 2;
                  }
                  if(preg_match(&quot;/Sec-WebSocket-Key: (.*)/&quot;,$buf,$match)){ 
                          $key =  base64_encode(hash(&quot;sha1&quot;,trim($match[1]).&quot;258EAFA5-E914-47DA-95CA-C5AB0DC85B11&quot;)); 
                          $response_header =    &quot;HTTP/1.1 101 Switching Protocols&quot;.&quot;\r\n&quot;.
                          &quot;Upgrade: websocket&quot;.&quot;\r\n&quot;.
                          &quot;Connection: Upgrade&quot;.&quot;\r\n&quot;.
                          &quot;Sec-WebSocket-Accept: $key&quot;.&quot;\r\n\r\n&quot;;
  
                          //SERVER RESPONSE ----
                          socket_write($msgsock,$response_header,strlen($response_header));
  
                          echo &quot;handshake done...&quot;;
                  };
          } while(1);
          socket_close($msgsock);
  } while(1);
  </code></pre>
  <p>i always have this error : <strong>failed: Error during WebSocket handshake: Invalid status line</strong>
  [1]: <a href="https://www.rfc-editor.org/rfc/rfc6455" rel="nofollow noreferrer">https://www.rfc-editor.org/rfc/rfc6455</a></p>
  <p>Here the HTTP request :</p>
  <p>the HTTP request :</p>
  <pre><code>Accept-Encoding:gzip, deflate, sdch
  Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,he;q=0.2
  Cache-Control:no-cache
  Connection:Upgrade
  Cookie:__utma=96992031.134451192.1399804258.1402844967.1402848436.4; __utmz=96992031.1399804258.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _ga=GA1.4.134451192.1399804258; toolbarDisplay=hide
  Host:127.0.0.1:1577
  Origin:http://127.0.0.1
  Pragma:no-cache
  Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits
  Sec-WebSocket-Key:2yODScjZV/dap0DsDSWDFQ==
  Sec-WebSocket-Version:13
  Upgrade:websocket
  User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36
  </code></pre>
  <p>Here the reponse generated by the PHP server :</p>
  <pre><code>HTTP/1.1 101 Switching Protocols
  Upgrade: websocket
  Connection: Upgrade
  Sec-WebSocket-Accept: MDQ1NjFhMzc1YjY5MWQwNTU1ZGIzNDYyZmM0YTc1ODFhMDBlMzdmOQ==
  </code></pre>