Improve error handling when connections are lost.