FTP Client reply sync
<p>I'm writting a FTP client in C# with a socket.
Now I have the following problem. When I send a FTP command through my control connection socket to the Server I try to receive the reply. But sometimes the server sends the data with delays.
My result are fragmented.</p>

<p>Now my code:</p>

<pre><code>public byte[] BinaryReceive()
{
       byte[] bytes;
       using (MemoryStream memoryStream = new MemoryStream())
             {
                        byte[] buffer = new byte[4056];
                        while (_controlConnectionSocket.Available &gt; 0)
                        {
                            int count = _controlConnectionSocket.Receive(buffer, buffer.Length, SocketFlags.None);
                            memoryStream.Write(buffer, 0, count);

                            Thread.Sleep(100);
                        }
                        bytes = memoryStream.ToArray();

              }
       return bytes;
 }
</code></pre>

<p>How can I avoid this situation? Currenty I implemented the <code>Thread.Sleep()</code> a as workaround. But this can not be the solution.</p>

<p>Any suggestion would be appreciated.</p>

<p><strong>Edit:</strong></p>

<p>I solved the problem. The key is, not to use <code>Available &gt; 0</code> instead I wait for the reply end. Than it works..</p>
