title: Socket connection to ftp not receiving answers
body: <p>I'm using C#, Visual Studio 2008, Framework 2.0 for embedded device.</p>
  
  <p>I'm having trouble with the use of a Socket connection to communicate with FTPs.</p>
  
  <p>In the purpose of you guys to help me, I wrote a snippet to explain my troubles...</p>
  
  <pre><code>        IPEndPoint endpoint;
          Socket cmdsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  
          IPAddress address = IPAddress.Parse(args[0]);
          endpoint = new IPEndPoint(address, int.Parse(args[1]));   
  
          // Connection
          cmdsocket.Connect(endpoint);
  
          if (cmdsocket.Connected)
          {
              do
              {
                  Console.WriteLine("Type the command to send : ");
                  string l_sCommandToSend = Console.ReadLine();
                  Console.WriteLine("Do you want a timeout to receive ? (Y/N (default))");
                  string l_sReceiveTimeout = Console.ReadLine();
  
                  // Sending
                  byte[] bytes = Encoding.ASCII.GetBytes((l_sCommandToSend + "\r\n").ToCharArray());
                  cmdsocket.Send(bytes, bytes.Length, 0);
                  if (l_sReceiveTimeout == "Y")
                  {
                      Thread.Sleep(1000);
                  }
  
                  // Receiving
                  byte[] bufferToRead = new byte[512];
                  string l_sResponsetext = "";
                  int l_iByteReceivedCount = 0;
                  while (cmdsocket.Available &gt; 0)
                  {
                      Console.WriteLine("Receiving...");
                      l_iByteReceivedCount = cmdsocket.Receive(bufferToRead, SocketFlags.None);
                      l_sResponsetext += Encoding.ASCII.GetString(bufferToRead, 0, l_iByteReceivedCount);
                      Console.WriteLine("Bytes Received : " + l_iByteReceivedCount);
                      Console.WriteLine("l_sResponsetext : " + l_sResponsetext);
                  }
                  Console.WriteLine("This is the answer : " + l_sResponsetext);
              } while (true);
          }
  
  
          Console.WriteLine("Job done.");
          Console.ReadLine();
  
          // Fermeture du socket
          cmdsocket.Close();
          cmdsocket = null;
  </code></pre>
  
  <p>Above is a snippet that :</p>
  
  <ul>
  <li>Connect to a FTP (I'm using FileZilla Server for tests purposes on my server side)</li>
  <li>Asks the user which command he wants to send</li>
  <li>Asks the user if he wants a Sleep between send and receive (More details later)</li>
  <li>Reads and displays the answer.</li>
  </ul>
  
  <p>I noticed that if I do not use a Sleep between a FTP command and the response, sometimes I don't get a correct response (empty string). 
  For example, sending the command "USER Andy" will result in the response "" when no Sleep is set. When the sleep is set to 1 second, I do get the proper answer which is "331 Password required for Andy".</p>
  
  <p>I tried to debug my Socket without talking to a FTP and chose a software like Hercules. Everything is working as expected, my Receive method call just hangs 'til it receives an answer.</p>
  
  <p>What am I doing wrong here ?</p>