Connection state

Connection states implemented:

- connected
- disconnected
- authenticated
- protocol_error

This should close #314.