Refactoring the socket clients to reuse code