Make detection of closed connections more robust.

Add temporary logging to help diagnose errors on travis.