title: How to send message in WebSocket
body: <p>I took a simple WebSocket server, where web-client sent message (Hello Wordld) and after the server respond to client and sent Hello World"". But I want to get a data from server and after client will print date on web -page. What I did wrong? Can you help me</p>
  
  <pre><code>namespace WebSocket
   {
  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 = "";
              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]                     // dGhlIHNhbXBsZSBub25jZQ== \r\n .......
                            .Replace("\r", "").Split('\n')[0]  // dGhlIHNhbXBsZSBub25jZQ==
                            .Trim();
  
                  // key should now equal dGhlIHNhbXBsZSBub25jZQ==
                  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
                       ;
  
                  // which one should I use? none of them fires the onopen method
                  client.Send(System.Text.Encoding.UTF8.GetBytes(response));
  
                  var i = client.Receive(buffer); // wait for client to send a message
  
                  // once the message is received decode it in different formats
                  Console.WriteLine(Convert.ToBase64String(buffer).Substring(0, i));
  
                  Console.WriteLine("\n\nPress enter to send data to client");
                  Console.Read();
  
                  var time = DateTime.Now.ToString();
  
                  int length = time.Length;
  
                  var Data = Encoding.UTF8.GetBytes(time);
  
                  buffer = Data;
  
  
                  var subA = SubArray&lt;byte&gt;(buffer, 0, length);
                  client.Send(subA);
                  Thread.Sleep(10000);//wait for message to be send
  
  
              }
          }
          catch (SocketException exception)
          {
              throw exception;
          }
          finally
          {
              if (serverSocket != null &amp;&amp; serverSocket.IsBound)
              {
                  serverSocket.BeginAccept(null, 0, OnAccept, null);
              }
          }
      }
  
      public static T[] SubArray&lt;T&gt;(T[] data, int index, int length)
      {
          T[] result = new T[length];
          Array.Copy(data, index, result, 0, length);
          return result;
      }
  
      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));
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is web-client</p>
  
  <pre><code>  &lt;script type="text/JavaScript"&gt;
      function connect() {
          var ws = new WebSocket("ws://localhost:8080/service");
          ws.onopen = function () {
              alert("About to send data");
              ws.send("Hello World"); 
              alert("Message sent!");
          };
  
          ws.onmessage = function (evt) {
             // alert("About to receive data");
              var received_msg = evt.data;
              document.write("Message received = "+received_msg);
              // alert("Message received = "+received_msg);
          };
          ws.onclose = function () {
              // websocket is closed.
              alert("Connection is closed...");
  
          };
      };
  &lt;/script&gt;
  </code></pre>
  
  <p>For example if I will delete this sentences </p>
  
  <pre><code> var time = DateTime.Now.ToString();
   int length = time.Length;
   var Data = Encoding.UTF8.GetBytes(time);
    buffer = Data;
  </code></pre>
  
  <p>and change the </p>
  
  <pre><code>                var subA = SubArray&lt;byte&gt;(buffer, 0, length);
  </code></pre>
  
  <p>after this everything is work, but it only send Hello World, but I want to send a time and date. How I can do it? Please help me</p>