Fixed bugs in bad connection detection

Now catches both errors and timeouts.