title: How does c# SslStream's read() function know when to return 0?
body: <p>I have an ssl client set up in c# and am currently taking steps to make sure socket reads are as reliable as they can be. The SslStream.read() documentation indicates that it returns 0 when there is no more data to read, what does that mean exactly? What if there is some lag so there is no data ready to be read? Does SslStream.read() have a built in timeout before it returns 0 or should I add some retries and what not until deciding the connection is lost?</p>
  
  <p>Here is my basic workflow for receiving data.</p>
  
  <pre><code>ulong expected = 8;
  int offset = 0;
  int read = 0;
  do
  {
      read = sslStream.Read(buffer, offset, expected - offset);
      offset += read;
  } while (offset != expected &amp;&amp; read != 0);
  
  expected = byte8toulong(buffer);
  Array.Clear(buffer, 0, buffer.Length);
  read = 0;
  offset = 0;
  
  do
  {
      // The same as above but saving the data as it comes in.
  } while(offset != expected &amp;&amp; read != 0);
  </code></pre>
  
  <p>So I guess my question is under what circumstances is read == 0? Does lag cause it to return 0? Should I add a timer / retries until I'm satisfied that the connection is hosed or does Read() already do something like that?</p>