DRYed up the OSError/socket.gaierror handler.