public void AddReplaceShape(UIFreeHandLine newShape)
 {
     if (newShape.Points.Count == 0)
     {
         return;
     }
     for (int i = Shapes.Count - 1; i >= 0; i--)
     {
         UIFreeHandLine fhl = Shapes[i];
         if (fhl.Points.Count > 0)
         {
             if (fhl.Points[0].I.Equals(newShape.Points[0].I))
             {
                 lock (Shapes) {
                     int pos = Shapes.IndexOf(fhl);
                     Shapes.RemoveAt(pos);
                     Shapes.Insert(pos, newShape);
                 }
                 return;
             }
         }
     }
     lock (Shapes) {
         Shapes.Add(newShape);
     }
 }
 public void SaveSession(string reason)
 {
     try {
         string FileName = "";
         if (reason.Equals("Autosave"))
         {
             FileName = ApplicationLocation.StartupPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + " " + SessionID + " " + reason + ".txt";
         }
         else
         {
             FileName = ApplicationLocation.StartupPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + " " + SessionID + " " + DateTime.Now.ToString("HH-mm-ss") + " " + reason + ".txt";
         }
         StreamWriter sw = new StreamWriter(FileName);
         for (int i = 0; i < Users.Count; i++)
         {
             User u = Users[i];
             sw.WriteLine("User: "******"Color: " + u.Shapes[0].Points[0].C.ToString());
             }
             for (int j = 0; j < u.Shapes.Count; j++)
             {
                 UIFreeHandLine fhl = u.Shapes[j];
                 sw.WriteLine(fhl.Points[0].X + "," + fhl.Points[0].Y + "," + fhl.Points[0].T.ToString("HH:mm:ss.fff"));
                 for (int k = 1; k < fhl.Points.Count; k++)
                 {
                     TimePoint tp  = fhl.Points[k];
                     TimePoint tp0 = fhl.Points[k - 1];
                     sw.WriteLine((tp.X - tp0.X) + "," + (tp.Y - tp0.Y) + "," + tp.T.Subtract(tp0.T).ToString());
                 }
                 sw.WriteLine();
             }
         }
         sw.WriteLine("---------------------------------------");
         sw.Close();
     }
     catch (Exception e) {
         ApplicationLocation.Log.WriteLine("Failed to save at " + DateTime.Now.ToString(ApplicationLocation.DATE_TIME_STRING_MAIN) + " : " + e.ToString());
     }
 }
        public NetworkObject CreateResponseFromRequest(NetworkObject NOReq)
        {
            NetworkObject NORsp = new NetworkObject();

            NORsp.NetworkCode = NOReq.NetworkCode;
            string eo1 = "", eo2 = "", eo3 = "", eo4 = "", eo5 = "";

            if (NOReq.ExchangeObjects.Count > 0)
            {
                eo1 = NOReq.ExchangeObjects[0].ToString();
            }
            if (NOReq.ExchangeObjects.Count > 1)
            {
                eo2 = NOReq.ExchangeObjects[1].ToString();
            }
            if (NOReq.ExchangeObjects.Count > 2)
            {
                eo3 = NOReq.ExchangeObjects[2].ToString();
            }
            if (NOReq.ExchangeObjects.Count > 3)
            {
                eo4 = NOReq.ExchangeObjects[3].ToString();
            }
            if (NOReq.ExchangeObjects.Count > 4)
            {
                eo5 = NOReq.ExchangeObjects[4].ToString();
            }
            switch (NOReq.NetworkCode)
            {
            case NetworkObject.REQUEST_STATE:
                if (!UserExists(eo1))
                {
                    UserAdd(eo1);
                }
                User u = UserGet(eo1);
//					switch (State) {
//						case STATE_INPUT:
//							NORsp.ExchangeObjects.Add(u.Shapes.Count == 0 ? STATE_INPUT : STATE_INPUT_WAITING);
                NORsp.ExchangeObjects.Add(GetColor(eo1).ToArgb());
//					NORsp.ExchangeObjects.Add(GenerateColor(Users.IndexOf(u)).ToArgb());
//							break;
//						case STATE_VERIFY:
//							NORsp.ExchangeObjects.Add(STATE_VERIFY);
                foreach (User uuu in Users)
                {
                    if (uuu.Shapes.Count > 0)
                    {
                        NORsp.ExchangeObjects.AddRange(uuu.Serialize());
//									Console.WriteLine("User {0} Shapes {1}", uuu.UserID, uuu.Shapes.Count);
                    }
                }
//							break;
//						default:
//
//							break;
//					}
                break;

            case NetworkObject.CLEAR_MY_SHAPES:
                User me = UserGet(eo1);
                SaveSession("Delete by " + me.UserID);
                me.Shapes.Clear();
//					State = STATE_INPUT;
                break;

            case NetworkObject.SEND_SHAPE:
                if (UserExists(eo1))
                {
                    User uu = UserGet(eo1);
//						uu.Shapes.Clear();
                    for (int i = 1; i < NOReq.ExchangeObjects.Count; i++)
                    {
                        UIFreeHandLine uif = new UIFreeHandLine();
                        uif.Deserialize(NOReq.ExchangeObjects[i].ToString());
                        uu.AddReplaceShape(uif);
//							Console.WriteLine("Shapes with the added / replaced one {0}", uu.Shapes.Count);
                    }
                }
                break;

            default:
                ApplicationLocation.Log.WriteLine("No such Message ID: " + NOReq.NetworkCode);
                break;
            }
            return(NORsp);
        }