added error handling around socket errors