better exception handling in socket operations