Refactor error handling when an IOException on a socket occurs