title: Linux/Mono throws ArgumentOutOfRangeException where Windows does not
body: <p>Trying to read data from a TcpClient buffer into a byte array:</p>
  
  <pre><code>private byte[] excessData;
  public bool receiveMessage(ref string recvStr, string daddy = "")
  {
      recvStr = "";
      CSocket_PacketProtocol packprot = new CSocket_PacketProtocol(0, this);
  
      WriteDebugWithTimestamp("receiveMessage obtaining client NetworkStream - daddy = " + daddy);
      NetworkStream nStream = client.GetStream();
      WriteDebugWithTimestamp("receiveMessage client NetworkStream obtained, entering receive loop");
  
      int bytesRead = 0;
      do
      {
          if (!packprot.done)
          {
              try
              {
                  byte[] inStream = new byte[10025]; //We'll read into this stream
  
                  int buffSize = client.ReceiveBufferSize;
                  WriteDebugWithTimestamp(String.Format("Starting nStream.Read"));
                  WriteDebugWithTimestamp(String.Format(String.Format("buffSize={0} -- inStream.Length={1}", buffSize, inStream.Length)));
  
                  bytesRead = nStream.Read(inStream, 0, buffSize); //Recieve data from stream to inStream, return amount of bytes read
  
                  WriteDebugWithTimestamp(String.Format("receiveMessage bytesread: {0}", bytesRead.ToString()));
                  WriteDebugWithTimestamp(String.Format("receiveMessage client.Connected: {0}", client.Connected.ToString()));
  
                  ../Cut
  </code></pre>
  
  <p>Where client is a <code>System.Net.Sockets.TcpClient</code>. The exception occurs at <code>nStream.Read</code>.</p>
  
  <p>Output when run by Debian-6.0_64 / Mono 2.11.2 as <code>Mono --runtime=v4.0.30319 Program.exe</code>:</p>
  
  <pre><code>receiveMessage obtaining client NetworkStream - daddy = 
  receiveMessage client NetworkStream obtained, entering receive loop
  receiveMessage this.excessData == null: True -- excessdata.length=n/a
  Starting nStream.Read
  buffSize=87380 -- inStream.Length=10025
  
  System.ArgumentOutOfRangeException: Argument is out of range.
  Parameter name: offset+size exceeds the size of buffer
    at System.Net.Sockets.NetworkStream.Read (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in &lt;filename unknown&gt;:0 
    at SocketOperations.CSocketOperations.receiveMessage (System.String&amp; recvStr, System.String daddy) [0x00000] in &lt;filename unknown&gt;:0 
    at SocketServerV3_Demo.CDownloadServer_ClientHandle..ctor (System.Net.Sockets.TcpClient client) [0x00000] in &lt;filename unknown&gt;:0 
    at SocketServerV3_Demo.Program.handleNewConnection (System.Net.Sockets.TcpClient client) [0x00000] in &lt;filename unknown&gt;:0 
  </code></pre>
  
  <p>The buffSize is way bigger than the byte array length, but that exception doesn't occur when I run the exact same program on windows (without mono, haven't tried with). What gives? And how do I fix this? Is it as simple as increasing the buffer size or is there something else going on? The reason I'm asking (rather than just trying it) is because I haven't got a clue what's going on and I want to make sure I get it done correctly.</p>
  
  <p>I'd like to find out the reason behind this behaviour, because seeing it like this I'd expect it to crash on windows as well. Unless the size of the buffer is determined by the OS and on Windows it's smaller/equal to the byte array size.. can anyone explain?</p>