title: Websocket server: onopen function on the web socket is never called
body: <p>I'm trying to implement a C# web socket server, but its giving me a few troubles.
  I'm running a webserver(ASP.NET) to host the page with the javascript and the web socket server is implemented as a C# console application.</p>
  <p>I'm able to detect the connection attempt from the client (chrome running the javascript) and also to retrieve the handshake from the client. But the client doesn't seem to accept the handshake I'm sending back (the <code>onopen</code> function on the web socket is never called).</p>
  <p>I've been reading the <a href="https://datatracker.ietf.org/doc/html/draft-hixie-thewebsocketprotocol-75" rel="nofollow noreferrer">The Web Socket protocol</a> and I can't see what I'm doing wrong.
  Heres a bit of the server code:</p>
  <pre><code>Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
  IPEndPoint ep = new IPEndPoint(IPAddress.Parse(&quot;127.0.0.1&quot;), 8181);
  listener.Bind(ep);
  listener.Listen(100);
  Console.WriteLine(&quot;Wainting for connection...&quot;);
  Socket socketForClient = listener.Accept();
  if (socketForClient.Connected)
  {
      Console.WriteLine(&quot;Client connected&quot;);
      NetworkStream networkStream = new NetworkStream(socketForClient);
      System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);
      System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream);
  
      //read handshake from client:
      Console.WriteLine(&quot;HANDSHAKING...&quot;);
      char[] shake = new char[255];
      streamReader.Read(shake, 0, 255);
  
      string handshake =
         &quot;HTTP/1.1 101 Web Socket Protocol Handshake\r\n&quot; +
         &quot;Upgrade: WebSocket\r\n&quot; +
         &quot;Connection: Upgrade\r\n&quot; +
         &quot;WebSocket-Origin: http://localhost:8080\r\n&quot; +
         &quot;WebSocket-Location: ws://localhost:8181\r\n&quot; +
         &quot;\r\n&quot;;
  
      streamWriter.Write(handshake);
      streamWriter.Flush();
  </code></pre>
  <p>I'm running to web server on port 8080 and the web socket server on port 8181, both on my localhost.</p>
  <p>I've tried sending the handshake in different encodings (ASCII, bytes and Hex) but this doesn't seem to make a difference. The connection is never fully established.
  The javascript looks like this:</p>
  <pre><code>var ws;
  var host = 'ws://localhost:8181';
  debug(&quot;Connecting to &quot; + host + &quot; ...&quot;);
  try {
   ws = new WebSocket(host);
  } catch (err) {
   debug(err, 'error');
  }
  ws.onopen = function () {
   debug(&quot;connected...&quot;, 'success');
  };
  ws.onclose = function () {
   debug(&quot;Socket closed!&quot;, 'error');
  };
  ws.onmessage = function (evt) {
   debug('response: ' + evt, 'response');
  };
  </code></pre>
  <p>I'm guessing that the error lies in the C# server as chrome is sending the information as it should, but as a said the <code>onopen</code> function is never called.</p>
  <p><strong>So my question in short:</strong>
  Has any of you ever accomplished this - and if yes, how did you do it?
  And of cause: Do you see any apparent errors in the code (hope thats not to much to ask)</p>