refactored socket connection