refactoring socket javascript