Added better socket error handling