Hi , if you are storing your messages in collection, it would be an easy stuff.
Lets say that you have List<chatmsg> _msgs, so storing your collection in MemoryStream will be performed by next code snippet:
using(MemoryStream ms=new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, _msgs);
writeStream.Close();
}
Hope , this will help.