internal static void syncCalendar()
        {
            DateTime?syncDate = Globals.ThisAddIn.last_calendar_sync;

            ASPECCalendarResponse aspecResponse = syncCalendar(syncDate, null, null);

            if (aspecResponse != null)
            {
                DateTime?lastModified = null;

                if (aspecResponse.last_modified != null)
                {
                    lastModified = convertASPECDateToDate(aspecResponse.last_modified);
                }

                if (syncDate == null && aspecResponse.batch_last_modified != null)
                {
                    syncDate = convertASPECDateToDate(aspecResponse.batch_last_modified);
                }

                int batchNumber = 1;
                int?batchCount  = aspecResponse.batch_count;

                while (batchCount != null && (batchNumber <= batchCount))
                {
                    aspecResponse = syncCalendar(syncDate, batchNumber, batchCount);

                    if (aspecResponse.last_modified != null)
                    {
                        DateTime?responseLastModified = convertASPECDateToDate(aspecResponse.last_modified);
                        if (lastModified == null || (responseLastModified != null && responseLastModified > lastModified))
                        {
                            lastModified = responseLastModified;
                        }
                    }

                    batchNumber++;
                }

                if (lastModified != null && lastModified.HasValue)
                {
                    Properties.Settings.Default.last_calendar_sync = lastModified.Value;
                    Globals.ThisAddIn.last_calendar_sync           = lastModified.Value;

                    Properties.Settings.Default.Save();
                }
            }
        }
        internal static ASPECCalendarResponse syncCalendar(DateTime?syncDate, int?batchNumber, int?batchCount)
        {
            byte[] postData = null;

            if (syncDate != null && syncDate.HasValue)
            {
                string dateString = convertDateToASPECDate(syncDate.Value);

                List <Reminder> reminders = null;
                if (batchNumber == null)
                {
                    reminders = getASPECCalendarItems(OlDefaultFolders.olFolderCalendar);

                    List <Reminder> taskReminders = getASPECCalendarItems(OlDefaultFolders.olFolderTasks);
                    if (taskReminders != null)
                    {
                        if (reminders == null)
                        {
                            reminders = new List <Reminder>();
                        }

                        reminders.AddRange(taskReminders);
                    }
                }

                if (reminders == null)
                {
                    reminders = new List <Reminder>();
                }

                String jsonRemindersString = JsonConvert.SerializeObject(reminders, Formatting.Indented);
                String requestParam        = String.Format("last_modified={0}&client_reminders={1}", dateString, jsonRemindersString);

                if (batchCount != null)
                {
                    requestParam += String.Format("&batch_count={0}&batch_number={1}", batchCount, batchNumber);
                }

                postData = new ASCIIEncoding().GetBytes(requestParam);

                //MessageBox.Show("POST DATA:" + requestParam);
            }

            string response = makeWebRequest(Globals.ThisAddIn.endpoint + (batchNumber == null ? CALENDAR_METHOD_BULK : CALENDAR_METHOD),
                                             "POST",
                                             postData,
                                             Globals.ThisAddIn.username,
                                             Globals.ThisAddIn.password);

            //MessageBox.Show(response);

            ASPECCalendarResponse aspecResponse = JsonConvert.DeserializeObject <ASPECCalendarResponse>(response);

            if (aspecResponse != null && aspecResponse.last_modified != null)
            {
                if (aspecResponse.server_reminders != null)
                {
                    DateTime currentTimestamp = DateTime.Now;
                    for (int i = 0; i < aspecResponse.server_reminders.Count <Reminder>(); i++)
                    {
                        DateTime modifiedTime = createOrUpdateReminder(aspecResponse.server_reminders[i]);

                        if (modifiedTime > currentTimestamp)
                        {
                            currentTimestamp = modifiedTime;
                        }
                    }

                    Properties.Settings.Default.last_calendar_sync_local = currentTimestamp;
                    Globals.ThisAddIn.last_calendar_sync_local           = currentTimestamp;

                    Properties.Settings.Default.Save();
                }
            }
            else
            {
                throw new System.Exception("Bad response from ASPEC");
            }

            return(aspecResponse);
        }