Refactoring Socket: refactor read/write functions