title: C# - Websocket - Sending Message Back To Client
body: <p>I've been working on a C# Web Socket server for roughly 24 hours.</p>
  <p>I've currently figured out how to complete a handshake and get the connection initialized.</p>
  <p>Also I've figured out how to take the <code>byte[]</code> data and decode it into a it's original string.</p>
  <p>But now I'm stuck and looking for help.</p>
  <p>I can't seem to figure out how to put together a proper data structure and send it back to the client.
  If you send the original data you received the WebSocket on the client-side will tell you the data cannot be masked <em>(which is why it needs to be decoded)</em>.</p>
  <p>So basically, what I'm asking in short is how do I structure response data to send back to the WebSocket client?</p>
  <p>I've been using <a href="https://www.rfc-editor.org/rfc/rfc6455" rel="nofollow noreferrer">https://www.rfc-editor.org/rfc/rfc6455</a> as a resource for my research.</p>
  <p>Please keep in mind that I'm just using a regular socket for this.</p>
  <p>Here is my decode code:</p>
  <pre><code>if (dataBuffer.Length &gt; 0)
  {
      if (dataBuffer[0] == 129)
      {
          int msg_length = dataBuffer[1] - 128;
          if (msg_length &lt;= 125)
          {
              // Msg ready to decode.
              Log.info(&quot;Message Length: &quot; + msg_length);
  
  
              Byte[] decoded = new Byte[dataBuffer.Length];
              Byte[] encoded = new Byte[dataBuffer.Length - 6];
  
              Array.Copy(dataBuffer, 6, encoded, 0, msg_length);
  
              Byte[] key = new Byte[4] { dataBuffer[2], dataBuffer[3], dataBuffer[4], dataBuffer[5] };
              for (int i = 0; i &lt; encoded.Length; i++)
              {
                  decoded[i] = (Byte)(encoded[i] ^ key[i % 4]);
              }
  
              Log.info(&quot;MSG: &quot; + Encoding.UTF8.GetString(decoded));
  
              byte[] return_msg = new byte[decoded.Length + 8];
  
              return_msg[0] = 1;
              return_msg[1] = 0;
              return_msg[2] = 0;
              return_msg[3] = 0;
              // OP Code
              return_msg[4] = 0x1;
              return_msg[5] = 0x0;
              return_msg[6] = 0x0;
              return_msg[7] = 0x0;
  
              Array.Copy(decoded, 0, return_msg, 8, decoded.Length);
  
              socket.Send(return_msg);
          }
          else if (msg_length == 126)
          {
              // Longer Message
              msg_length = dataBuffer[2] + dataBuffer[3];
  
              Log.info(&quot;Message Length: &quot; + msg_length);
  
              Byte[] key = new Byte[4] { dataBuffer[4], dataBuffer[5], dataBuffer[6], dataBuffer[7] };
  
              Byte[] decoded = new Byte[dataBuffer.Length];
              Byte[] encoded = new Byte[dataBuffer.Length - 8];
  
              Array.Copy(dataBuffer, 8, encoded, 0, msg_length);
  
              for (int i = 0; i &lt; encoded.Length; i++)
              {
                  decoded[i] = (Byte)(encoded[i] ^ key[i % 4]);
              }
  
              Log.info(&quot;MSG: &quot; + Encoding.UTF8.GetString(decoded));
              byte[] return_msg = new byte[decoded.Length + 4];
  
              return_msg[0] = 129;
              return_msg[1] = 0;
              return_msg[2] = 0;
              return_msg[3] = 0;
                      
              Array.Copy(decoded,0,return_msg,4,decoded.Length);
  
              socket.Send(return_msg);
          }
          else if (msg_length == 127)
          {
              // Huge Message:
              Log.info(&quot;BIG MESSAGE&quot;);
          }
      }
  
  }
  </code></pre>