title: Unable to connect to the remote server at System.Net.FtpWebRequest.GetRequestStream()
body: <p>I'm able to FTP to the server and upload files while running VS2013 in Debug, but when I compile as Release | x86 I get the error "Unable to connect to the remote server" at the line that calls System.Net.FtpWebRequest.GetRequestStream(). 
  I can get to the FTP and upload files using WinSCP. I've run WireShark and seen no packets at all from the Release version of my code but saw all relevant packets when I executed from Debug mode within VS2013. I've got no firewall and it's really got me stumped. Any help would be greatly appreciated. - Chuck</p>
  
  <pre><code>    public bool UploadToFtp(string sFtpServer, string sFtpUserName, string sFtpPassword, string sSourceLocationPath, string sFtpDestinationPath, StringBuilder sbLogMessage)
      {
          bool bUpload;
          try
          {
              var oFile = new FileInfo(sSourceLocationPath);
              var oUri = new Uri(sFtpServer + sFtpDestinationPath + oFile.Name);
              var oFtp = (FtpWebRequest)WebRequest.Create(oUri);
              oFtp.Credentials = new NetworkCredential(sFtpUserName, sFtpPassword);
              oFtp.KeepAlive = false;
              oFtp.UseBinary = true;
              oFtp.UsePassive = true;
              oFtp.Timeout = 10000000;
              oFtp.ReadWriteTimeout = 10000000;
              oFtp.Method = WebRequestMethods.Ftp.UploadFile;
              oFtp.ContentLength = oFile.Length;
              var arrContent = File.ReadAllBytes(oFile.FullName);
              using (var oFileStream = oFile.OpenRead())
              {
                  using (var oStream = oFtp.GetRequestStream())
                  {
                      int iReadBytes;
                      do
                      {
                          iReadBytes = oFileStream.Read(arrContent, 0, int.Parse(oFile.Length.ToString(CultureInfo.InvariantCulture)));
                          oStream.Write(arrContent, 0, iReadBytes);
                      } while (iReadBytes != 0);
  
                      bUpload = true;
                      oStream.Flush();
                      oStream.Close();
                      oFileStream.Close();
                  }
              }
              File.Delete(sSourceLocationPath + oFile.Name);
          }
          catch (Exception ex)
          {
              LogMessage(ex.Message);
              bUpload = false;
          }
          return bUpload;
      }
  </code></pre>
  
  <p>***** EDIT *****
  I created a network trace log as suggested and got the following output (NOTE: I have changed the actual IP # and filename in this listing):</p>
  
  <pre><code>System.Net Verbose: 0 : [27252] WebRequest::Create(ftp://1.2.3.4//file.zip)
  System.Net Information: 0 : [27252] FtpWebRequest#28316044::.ctor(ftp://1.2.3.4//file.zip)
  System.Net Verbose: 0 : [27252] Exiting WebRequest::Create()    -&gt; FtpWebRequest#28316044
  System.Net Verbose: 0 : [27252] FtpWebRequest#28316044::GetRequestStream()
  System.Net Information: 0 : [27252] FtpWebRequest#28316044::GetRequestStream(Method=STOR.)
  System.Net Information: 0 : [27252] Current OS installation type is 'Client'.
  System.Net Verbose: 0 : [27252] ServicePoint#39974954::ServicePoint(1.2.3.4:21)
  System.Net.Sockets Verbose: 0 : [27252] Socket#24230272::Socket(AddressFamily#2)
  System.Net.Sockets Verbose: 0 : [27252] Exiting Socket#24230272::Socket() 
  System.Net.Sockets Verbose: 0 : [27252] Socket#16745860::Socket(AddressFamily#23)
  System.Net.Sockets Verbose: 0 : [27252] Exiting Socket#16745860::Socket() 
  System.Net.Sockets Verbose: 0 : [27252] DNS::TryInternalResolve(1.2.3.4)
  System.Net.Sockets Verbose: 0 : [27252] Socket#24230272::Connect(1.2.3.4:21#1294145406)
  System.Net.Sockets Error: 0 : [27252] Socket#24230272::UpdateStatusAfterSocketError() - TimedOut
  System.Net.Sockets Error: 0 : [27252] Exception in Socket#24230272::Connect - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 1.2.3.4:21.
  System.Net Error: 0 : [27252] Exception in FtpWebRequest#28316044::GetRequestStream - Unable to connect to the remote server.
     at System.Net.FtpWebRequest.GetRequestStream()
  System.Net Verbose: 0 : [27252] Exiting FtpWebRequest#28316044::GetRequestStream() 
  </code></pre>