Factor out server connection code to class