Refactored code for inserting messages to database

Fixes a bug causing the client to disconnect from server.
Updated project file
Refactoring
Refactoring