Added socket error handling