title: Error 1006 while sending message from server [C#] to client [HTML5 websocket]
body: <p>I've got a Big problem.
  I'm trying to establish a connection between c# socket and HTML5 WebSocket</p>
  
  <p>Server Code</p>
  
  <pre><code>using System;
  using System.Net.Sockets;
  using System.Net;
  using System.Security.Cryptography;
  using System.Threading;
  using System.Text;
  
  namespace Server_CSharp_HTML5
  {
      class Program
      {
          static Socket serverSocket = new Socket(AddressFamily.InterNetwork, 
          SocketType.Stream, ProtocolType.IP);
          static private string guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
  
          static void Main(string[] args)
          {            
              serverSocket.Bind(new IPEndPoint(IPAddress.Any, 8080));
              serverSocket.Listen(128);
              serverSocket.BeginAccept(null, 0, OnAccept, null);            
              Console.Read();
          }
  
          private static void OnAccept(IAsyncResult result)
          {
              byte[] buffer = new byte[1024];
              try
              {
                  Socket client = null;
                  string headerResponse = "";
                  string testMessage = "Hello World!";
                  if (serverSocket != null &amp;&amp; serverSocket.IsBound)
                  {
                      client = serverSocket.EndAccept(result);
                      var i = client.Receive(buffer);
                      headerResponse = (System.Text.Encoding.UTF8.GetString(buffer)).Substring(0,i);
                      // write received data to the console
                      Console.WriteLine(headerResponse);
  
                  }
                  if (client != null)
                  {
                      /* Handshaking and managing ClientSocket */
  
                      var key = headerResponse.Replace("ey:", "`")
                                .Split('`')[1]                    
                                .Replace("\r", "").Split('\n')[0]  
                                .Trim();
  
                      var test1 = AcceptKey(ref key);
  
                      var newLine = "\r\n";
  
                      var response = "HTTP/1.1 101 Switching Protocols" + newLine
                           + "Upgrade: websocket" + newLine
                           + "Connection: Upgrade" + newLine
                           + "Sec-WebSocket-Accept: " + test1 + newLine + newLine
                           //+ "Sec-WebSocket-Protocol: chat, superchat" + newLine
                           //+ "Sec-WebSocket-Version: 13" + newLine
                           ;
                      client.Send(System.Text.Encoding.UTF8.GetBytes(response));
  
                      var i = client.Receive(buffer); // wait for client to send a message
  
                      Console.WriteLine(Convert.ToBase64String(buffer).Substring(0, i));
                      Console.WriteLine(Convert.ToString(Convert.ToBase64String(buffer).Substring(0, i)));
                      Console.WriteLine(i.ToString());
                      Send(client, Encoding.UTF8.GetBytes(testMessage), 0, (testMessage).Length , 10000);
  
  
  
                  }
              }
              catch (SocketException exception)
              {
                  throw exception;
              }
              finally
              {
                  if (serverSocket != null &amp;&amp; serverSocket.IsBound)
                  {
                      serverSocket.BeginAccept(null, 0, OnAccept, null);
                  }
              }
          }
  
          private static string AcceptKey(ref string key)
          {
              string longKey = key + guid;
              byte[] hashBytes = ComputeHash(longKey);
              return Convert.ToBase64String(hashBytes);
          }
  
          static SHA1 sha1 = SHA1CryptoServiceProvider.Create();
          private static byte[] ComputeHash(string str)
          {
              return sha1.ComputeHash(System.Text.Encoding.ASCII.GetBytes(str));
          }
  
          public static void Send(Socket socket, byte[] buffer, int offset, int size, int timeout)
          {
              int startTickCount = Environment.TickCount;
              int sent = 0;  // how many bytes is already sent
              do
              {
                  if (Environment.TickCount &gt; startTickCount + timeout)
                      throw new Exception("Timeout.");
                  try
                  {
                      sent += socket.Send(buffer, offset + sent, size - sent, SocketFlags.None);
                  }
                  catch (SocketException ex)
                  {
                      if (ex.SocketErrorCode == SocketError.WouldBlock ||
                          ex.SocketErrorCode == SocketError.IOPending ||
                          ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
                      {
                          // socket buffer is probably full, wait and try again
                          Thread.Sleep(30);
                      }
                      else
                          throw ex;  // any serious error occurr
                  }
              } while (sent &lt; size);
          }
  
      }
  }
  </code></pre>
  
  <p>When I try to send a message <code>string testMessage = "Hello World!";</code>
  the client give me error, the error code given is 1006.
  I've searched through StackOverFlow and some forums but I can't find what i need.
  Hope that someone can help me.</p>
  
  <p>Regards, 50Special</p>