factoring out code for connecting to wave