Added better exception handling for failed socket access