improve error handling with socket creation