title: C# - Sending a WebSocket Message to the client
body: <p>I followed the following tutorial to setup a websocket server in C#:
  <a href="https://developer.mozilla.org/pt-BR/docs/WebSockets/Writing_WebSocket_server" rel="nofollow">https://developer.mozilla.org/pt-BR/docs/WebSockets/Writing_WebSocket_server</a></p>
  
  <p>After this, i wrote a part where it would decode a normal message and display it to me in a console.</p>
  
  <p>Now, i want to be able to send this message back to the client (exactly what ws://echo.websocket.org does)</p>
  
  <p>This is my code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Net.Sockets;
  using System.Net;
  using System.Text.RegularExpressions;
  using System.Security.Cryptography;
  
  using System.Threading.Tasks;
  
  namespace sockecho
  {
      class Program
      {
          static void Main(string[] args)
          {
              TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 80);
  
              server.Start();
              Console.WriteLine("Server has started on 127.0.0.1:80.{0}Waiting for a connection...", Environment.NewLine);
              TcpClient client = server.AcceptTcpClient();
              Console.WriteLine("A client connected.");
              NetworkStream stream = client.GetStream();
  
              //enter to an infinite cycle to be able to handle every change in stream
              while (true)
              {
  
                  while (!stream.DataAvailable) ;
                  Byte[] bytes = new Byte[client.Available];
                  stream.Read(bytes, 0, bytes.Length);
                  //translate bytes of request to string
                  String data = Encoding.UTF8.GetString(bytes);
                  if (new Regex("^GET").IsMatch(data))
                  {
                      #region startdata
                      Byte[] response = Encoding.UTF8.GetBytes("HTTP/1.1 101 Switching Protocols" + Environment.NewLine + "Connection: Upgrade" + Environment.NewLine + "Upgrade: websocket" + Environment.NewLine + "Sec-WebSocket-Accept: " + Convert.ToBase64String(
              SHA1.Create().ComputeHash(
                  Encoding.UTF8.GetBytes(
                      new Regex("Sec-WebSocket-Key: (.*)").Match(data).Groups[1].Value.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
                  )
              )
          ) + Environment.NewLine
          + Environment.NewLine);
  
                      stream.Write(response, 0, response.Length);
  
                      string responsel = Encoding.UTF8.GetString(response);
                      Console.WriteLine(responsel);
                      #endregion
                  }
                  else
                  {
                      //stream.Write(bytes, 0, bytes.Length);
                      string decodedmessage = GetMessage(bytes);
                      Console.WriteLine(decodedmessage);
  
                      byte[] toBytes = Encoding.ASCII.GetBytes(decodedmessage);
                      stream.Write(toBytes, 0, toBytes.Length);
  
                  }
  
              }
  
  
          }
  
          private static string GetMessage (Byte[] bytes)
          {
              string DETEXT = "";
              if (bytes[0] == 129)
              {
                  int position = 0;
                  int Type = 0;
                  ulong length = 0;
                  if (bytes[1] - 128 &gt;= 0 &amp;&amp; bytes[1] - 128 &lt;= 125)
                  {
                      length = (ulong)bytes[1] - 128;
                      position = 2;
                  }
                  else if (bytes[1] - 128 == 126)
                  {
                      Type = 1;
                      length = (ulong)256 * bytes[2] + bytes[3];
                      position = 4;
                  }
                  else if (bytes[1] - 128 == 127)
                  {
                      Type = 2;
                      for (int i = 0; i &lt; 8; i++)
                      {
                          ulong pow = Convert.ToUInt64(Math.Pow(256, (7 - i)));
                          length = length + bytes[2 + i] * pow;
                          position = 10;
                      }
                  }
                  else
                  {
                      Type = 3;
                      Console.WriteLine("error 1");
                  }
  
                  if (Type &lt; 3)
                  {
                      Byte[] key = new Byte[4] { bytes[position], bytes[position + 1], bytes[position + 2], bytes[position + 3] };
                      Byte[] decoded = new Byte[bytes.Length - (4 + position)];
                      Byte[] encoded = new Byte[bytes.Length - (4 + position)];
  
                      for (long i = 0; i &lt; bytes.Length - (4 + position); i++) encoded[i] = bytes[i + position + 4];
  
                      for (int i = 0; i &lt; encoded.Length; i++) decoded[i] = (Byte)(encoded[i] ^ key[i % 4]);
  
                      DETEXT = Encoding.UTF8.GetString(decoded);
                  }
              }
              else
              {
                  Console.WriteLine("error 2: " + bytes[0].ToString());
              }
              return DETEXT;
          }
      }
  }
  </code></pre>