“Unable to connect to the remote server Error”, this error occurs because the connection is lost with the server. When you make authenticated calls, the client opens the connection and then closes it as well. The connection is not re-used. If you make such calls repeatedly to the server, all available connections get exhausted. This can be fixed by modifying one of the TCP/IP parameters to increase the # of connections available. You would also need to insert a delay in your code using the Sleep statement to reduce the number of calls that are being made repeated to the server.
Solution:
1. Open registry editor.
2. Go to the following directory path to navigate to the registry key: “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters”
3. Look for the TcpTimedWaitDelay key name. If not found, add it by selecting Edit > New > DWORD Value from the menu bar. Rename the key name to “TcpTimedWaitDelay”. (see below the screen)
4. Double-click inside the right pane again to set the value of TcpTimedWaitDelay. Select Decimal as the Base, and enter 30 in the Value data field.(see same as below the screen)
5. Look the MaxUserPort key name. If not found, add it by selecting Edit > New > DWORD Value from the menu bar named “MaxUserPort”. Set its value to a decimal value between 5000 and 65534