Only connect to server when necessary