title: Improve Socket performance
body: <p>I have a TCP/IP socket and communicate to an external sensor device over it. The proprietary protocol that we use has a feature that the device can send a packet to the client at any time. This is called an "event" and is very much like a C# event.</p>
  
  <p>I basically implemented it like this:</p>
  
  <pre><code>...
  socket.ReceiveTimeout = ResolveTimeout(timeout);
  while (...)
  {
      var buffer = new byte[count];
      int readBytes = socket.Receive(buffer, 0, count, SocketFlags.None);
      ...
  }
  </code></pre>
  
  <p>With this code, the application suffers bad performance. I measured it in a profiler and it tells me that Socket.Receive needs 45% wall time.</p>
  
  <p>Is there any way how I can improve this?</p>