title: (Indy) TIdHTTP EIdSocketError Socket Error # 0 exceptions when downloading files
body: <p>I have this delphi code that basically <strong>download</strong> files from a secure server (using Indy build 10.5.8 r4743 if I'm not mistaken):</p>
  
  <p><strong>The problem is:</strong> I'm getting random "Socket Error # 0" exceptions that I couldn't get fix or even understand:</p>
  
  <p><code>Project MyProject.exe raised exception class EIdSocketError with message 'Socket Error # 0</code></p>
  
  <p><a href="http://dug.im/04607" rel="nofollow noreferrer">Stack is here</a>, pascal code is:</p>
  
  <pre><code>IdHTTP            := TIdHTTP.Create(nil);
  TheSSL            := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  TheCompressor     := TIdCompressorZLib.Create(nil);
  TheCookieManager  := TIdCookieManager.Create(IdHTTP);
  
  try SetupWebComponents(IdHTTP, TheSSL, TheCompressor, TheCookieManager); except end;
  
  TheCookieManager.OnNewCookie := MainForm.SyncNewCookie;
  
  // Go!
  Stream            := TMemoryStream.Create;
  try
     IsError        := False;
     try
        with IdHTTP do
        begin
             OnWork                := MainForm.IdHTTPWork_Download;
             try
                try
                   IsNewFile       := (Not FileExists(LocalFile));
                   if IsNewFile then
                      TheFile      := TFileStream.Create(LocalFile, fmCreate)
                   else
                       // File already exist, resume download
                       TheFile     := TFileStream.Create(LocalFile, fmOpenReadWrite);
  
                   DoExit          := False;
  
                   // Use [ Request.Referer ] it to pass VersionGUID to Work event (to record download progress in db)
                   Request.Referer := VersionGUID;
                   repeat
                         // Get resume byte
                         if IsNewFile then
                         begin
                              ResumeByte := 0;
                              IsNewFile  := False;
                         end
                         else
                             ResumeByte  := GetResumeByteFromDB();
  
                         if FileExists(LocalFile) then
                         begin
                              // File already exist, resume download
                              DoExit     := (ResumeByte &gt;= TheFileSize);
                              TheFile.Seek(ResumeByte, soFromBeginning);
                         end;
  
                         // Save ResumeByte, it will be used to record download progress in db &amp; eventually use it to resume downloads)
                         IdHTTP.Tag      := ResumeByte;
  
                         Request.Range := IntToStr(ResumeByte) + '-';
  
                         Get(TheURL, TheFile);
                         IsError         := (Not (ResponseCode in [200, 206])) OR (Pos('login', URL.Document) &lt;&gt; 0);
  
                         // Break if there's any error (to allow retrying later in a clean fashion)
                         if IsError then
                            Break;
                   until DoExit;
  
                   Disconnect;
                except
                      if (ResponseCode = 404) then
                      begin
                           // File doesn't exist, get out
                           Result        := False;
                           Exit;
                      end;
                end;    // try/except
             finally
                    FreeAndNil(TheFile);
             end;    // try/finally
        end;    // with
     except
           IsError  := True;
     end;    // try/except
  finally
         Stream.Free;
  end;
  </code></pre>
  
  <p>I <a href="https://stackoverflow.com/questions/10326810/random-indy-responsecode-1-eidsocketerror-socket-error-0-exceptions">posted a similar question some time ago</a>, but that was regarding <strong>upload, not download</strong>. The code was fixed since then with the kind help of SO members, and the same code (used to deal with cookies, re-login, etc...) is being used now so I assume the problem is really in the download procedure shown above</p>
  
  <p><strong>Can someone please take a look at this tell me what I'm doing wrong?</strong></p>