Write the code to handle most of the events (except send/receive messages). Not tested yet.