title: How to handle empty socket buffer
body: <p>I am writing a socket server and I noticed that if it received an empty socket buffer, it will cause a socketexception. In the receive call, how can I detect and handle the empty buffer and send a -1 response back to the client before the socket closes?</p>
  
  <p>Code:</p>
  
  <pre><code>try
  {
      byte[] byteBuffer = new Byte[1024];
      int size = m_clientSocket.Receive(byteBuffer);
      if (size &gt; 0)
      {
          ParseReceiveBuffer(byteBuffer, size);
      }
      else
      {
          m_clientSocket.Send(BitConverter.GetBytes(-1));
      }
  }
  catch (SocketException ex)
  {
      if (ex.SocketErrorCode == SocketError.WouldBlock ||
          ex.SocketErrorCode == SocketError.IOPending ||
          ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
      {
          // socket buffer is probably empty, wait and try again
          Thread.Sleep(1000);
      }
      // connection was unexpectively closed
  }
  </code></pre>