refactor sockets code