outsource the websocket connection code into a method