Refactor server connection