title: Slow down in TCP/IP receive speed
body: <p>I've written a simple .Net client that connects to a hardware device (FPGA) over TCP/IP. When I click a button in the client it sends a small (4 byte) "request" to the device, then immediately reads a block of data (approx 32kb) that the device responds with. The client code looks something like this:-</p>
  
  <pre><code>var stream = _tcpClient.GetStream();
  
  stream.Write(requestData, 0, requestData.Length);
  
  using (var ms = new MemoryStream())
  {
      var tempBuffer = new byte[65535];
      do
      {
          var numBytesRead = stream.Read(tempBuffer, 0, tempBuffer.Length);
          ms.Write(tempRead, 0, numBytesRead);
      }
      while(ms.Length &lt; ExpectedResponseSize);
  
      _hardwareResponse = ms.ToArray();
  }
  </code></pre>
  
  <p>Using a Stopwatch in the above code typically reports 2-3ms to read the entire 32kb response back, and this timing remains consistent if I repeatedly click the button slowly (e.g. once per second). </p>
  
  <p>If I start clicking the button more rapidly (e.g. every half a second) then after a few seconds the timings suddenly drop to around 12ms and stay there, even if I go back to clicking the button slowly. If I close then reopen the connection on the client and try again, it's back to 2-3ms times.</p>
  
  <p>WireShark shows 3-4 ACKs coming out of the PC during the faster responses, but this increases to a dozen or more once the timing drops to 12ms. In both cases the number and size of packets coming from the FPGA is the same. I'm as confident as I can be that it's not a problem in the code on the client or FPGA (neither can get much simpler) - gut feeling is that it's a protocol or network thing. Any thoughts? </p>