Add check for JSON type, frees message buffer
Added more error handling in client comms