Don't try to send when there is no connection
Improved message receiving

Improved code structure and made sure we don't try to receive when there
is no connection.