Added buffer clearing on failed serialization (prevents from sending incomplete messages)