refactored server-side socket code for greater modularity