/// <summary>
        /// Fetch a page of archived messages
        /// </summary>
        /// <param name="pageRequest">Paging options</param>
        /// <param name="with">Optional filter to only return messages if they match the supplied JID</param>
        /// <param name="roomId">Optional filter to only return messages if they match the supplied JID</param>
        /// <param name="start">Optional filter to only return messages whose timestamp is equal to or later than the given timestamp.</param>
        /// <param name="end">Optional filter to only return messages whose timestamp is equal to or earlier than the timestamp given in the 'end' field.</param>
        internal Task <XmppPage <Im.Message> > GetArchivedMessages(XmppPageRequest pageRequest, Jid with = null, Jid roomId = null, DateTimeOffset?start = null, DateTimeOffset?end = null)
        {
            Core.Iq iq = im.IqRequest(Core.IqType.Get, null, im.Jid, Xml.Element("query", xmlns));
            if (iq.Type == Core.IqType.Result)
            {
                var query = iq.Data["query"];
                if (query == null || query.NamespaceURI != xmlns)
                {
                    throw new XmppException("Erroneous server response.");
                }

                DataForm form = DataFormFactory.Create(query["x"]);

                string queryId = Guid.NewGuid().ToString().Replace("-", "");

                var request = Xml.Element("query", xmlns)
                              .Attr("queryid", queryId)
                              .Child(pageRequest.ToXmlElement());
                var filterForm = new SubmitForm();

                foreach (var campo in form.Fields)
                {
                    if (campo.Type == DataFieldType.Hidden)
                    {
                        filterForm.Fields.Add(campo);
                    }
                    if (campo.Name == "with" && with != null)
                    {
                        filterForm.AddUntypedValue("with", with);
                    }
                    if (campo.Name == "start" && start.HasValue)
                    {
                        filterForm.AddUntypedValue("start", DateTimeProfiles.ToXmppDateTimeString(start.Value));
                    }
                    if (campo.Name == "end" && end.HasValue)
                    {
                        filterForm.AddUntypedValue("end", DateTimeProfiles.ToXmppDateTimeString(end.Value));
                    }
                }

                request.Child(filterForm.ToXmlElement());


                var tcs       = new TaskCompletionSource <XmppPage <Im.Message> >();
                var queryTask = pendingQueries[queryId] = new ArchiveQueryTask(tcs);

                im.IqRequestAsync(Net.Xmpp.Core.IqType.Set, roomId, null, request, null,
                                  (string id, Core.Iq response) =>
                {
                    if (response.Type == Core.IqType.Error)
                    {
                        queryTask.SetException(Util.ExceptionFromError(response, "Failed to get archived messages"));
                    }
                    else
                    {
                        TryFinaliseQuery(response.Data);
                    }
                });

                return(tcs.Task);
            }
            else
            {
                var error = iq.Data["error"];
                throw new Exception(error["text"].InnerText);
            }
        }
 /// <summary>
 /// Invoked when an IQ stanza is being received.
 /// </summary>
 /// <param name="stanza">The stanza which is being received.</param>
 /// <returns>true to intercept the stanza or false to pass the stanza
 /// on to the next handler.</returns>
 public bool Input(Core.Iq stanza)
 {
     return(TryFinaliseQuery(stanza.Data));
 }