extract server communication code from main file