complete connect to server function.