Register for AppMessage callbacks before opening a connection as suggested by the documentation.
Fix a possible buffer overflow.