Added socket error checking