extract sendMessage function

also lay the framework for a queue of messages that should be sent when
a connection re-connects