Added handling of socket exceptions