title: Websocket security (wss) connection with PHP
body: <p>I'm starting with websockets and I learned how to do handshake. This code does handshake without SSL, I added SSL cert to my server and it doesn't work now:</p>
  
  <pre><code>$host = '176.xxx.xx.xx';
  $port = 44444;
  
  //Create TCP/IP sream socket and return the socket resource
  $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  
  // Bind the source address
  socket_bind($socket, $host, $port);
  
  // Listen to incoming connection
  echo "listening\r\n";
  socket_listen($socket);
  
  // Accept new connections
  $resource = socket_accept($socket);
  
  echo $headers = socket_read($resource, 1024);
  
  preg_match('/Sec-WebSocket-Key\: (.+?)\r\n/', $headers, $key);
  
  $acceptKey = $key[1].'258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
  $acceptKey = base64_encode(sha1($acceptKey, true));
  
  $upgrade = "HTTP/1.1 101 Switching Protocols\r\n".
             "Upgrade: websocket\r\n".
             "Connection: Upgrade\r\n".
             "Sec-WebSocket-Accept: $acceptKey".
             "\r\n\r\n";
  $r1 = socket_write($resource,$upgrade,strlen($upgrade));
  
  socket_close($resource);
  </code></pre>
  
  <p>I know it is not perfect code - I can improve it yourself, but I can't solve SSL problem yourself. So please focus on that.</p>
  
  <p>I was searching a lot, but I found only very complicated solution and I can't use them at all.  I have to use .pem file with my cert and private key, but how should I add it to that socket? Please don't give me solution with node.js or socket.io, it has to be made with PHP. Moreover, I prefer to not use any complicated classes, because I want to know bases and create my own class - the reason is that I want to use websockets for very specififc problem.</p>
  
  <p>Thank you a lot!</p>