Index: src/connection.cpp
===================================================================
--- src/connection.cpp	(revision 1659)
+++ src/connection.cpp	(working copy)
@@ -17,6 +17,7 @@
  */
 
 #include <iostream>
+#include <fstream>
 
 #include "error.hpp"
 #include "connection.hpp"
@@ -241,6 +242,9 @@
 		if(e.get_code() == error::WOULD_BLOCK)
 			return;
 #endif
+		std::ofstream out("connlost.txt");
+		out << "Connection error: " << "do_io threw: " << e.get_code() << std::endl;
+		out.close();
 		// We should not throw any error here because it would fall
 		// through to the selector. If something went wrong, then
 		// we have to handle it here.
@@ -277,6 +281,10 @@
 
 		if(bytes == 0)
 		{
+			std::ofstream out("connlost.txt");
+			out << "Connection error: " << "Remote site closed connection (in)" << std::endl;
+			out.close();
+
 			on_close();
 			return;
 		}
@@ -374,6 +382,9 @@
 
 		if(bytes <= 0)
 		{
+			std::ofstream out("connlost.txt");
+			out << "Connection error: " << "Remote site closed connection (out " << bytes << ")" << std::endl;
+			out.close();
 			on_close();
 			return;
 		}
@@ -398,6 +409,9 @@
 		}
 		else if(keepalive == KEEPALIVE_WAITING)
 		{
+			std::ofstream out("connlost.txt");
+			out << "Connection error: " << "Timeout" << std::endl;
+			out.close();
 			// Did not get a response since 30 seconds
 			on_close();
 		}
@@ -405,6 +419,14 @@
 
 	if(io & IO_ERROR)
 	{
+		int errval;
+		socklen_t errsize = sizeof(errval);
+		getsockopt(remote_sock->cobj(), SOL_SOCKET, SO_ERROR, &errval, &errsize);
+
+		std::ofstream out("connlost.txt");
+		out << "Connection error: " << "IO_ERROR: " << errval << std::endl;
+		out.close();
+
 		on_close();
 	}
 }

