add check for Socket errors and add the 401 status code check