Came across this issue for the 2nd time, so thought it was worth writing about since google doesn’t really turn up much.
I’d written a .NET4 c# console app that connects to Salesforce’s API and queries for specific record entities that have been modified. The console app was set to compile for Any CPU and was always running x64 on my laptop and the server. I extended the application to query the Salesforce Opportunity entity, which worked fine on the laptop but not on the server (Microsoft Windows Server 2008 R2 Standard).
The server returned the following error;
System.InvalidOperationException: There is an error in XML document (1, 3739). ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
So it was obviously a machine specific issue, since it worked on my laptop (and another server). But there really wasn’t much else to help, so troubleshooting then begins.
The end result is that it transpires running the console app as complied x86 only (forcing 32 bit running) fixes the issues.
Hope this helps someone.