refactor socket connection