Refactor client-side socket code.