refactor some duplicate code to abstract socket class