Improved type checking for socket use