protected ImapWebResponse GetCopyResponse(ImapSequenceSet requestUidSet)
        {
            ImapCopiedUidSet copiedUids;
              ImapMailbox createdMailbox = null;

              var mailbox = GetDestinationMailbox();
              var response = new ImapWebResponse(AllowCreateMailbox
                                         ? Session.Copy(requestUidSet, mailbox, out copiedUids, out createdMailbox)
                                         : Session.Copy(requestUidSet, mailbox, out copiedUids));

              if (response.Result.Succeeded) {
            if (createdMailbox != null) {
              if (Subscription) {
            Session.Subscribe(createdMailbox);
            Session.Lsub(createdMailbox);
              }
              else {
            Session.List(createdMailbox);
              }

              response.Mailboxes = new[] {createdMailbox};
            }

            if (copiedUids != null) {
              var builder = new ImapUriBuilder(DestinationUri);

              builder.UidValidity = copiedUids.UidValidity;

              if (copiedUids.AssignedUidSet.IsSingle)
            builder.Uid = copiedUids.AssignedUidSet.ToNumber();
              else
            builder.SearchCriteria = ImapSearchCriteria.Uid(copiedUids.AssignedUidSet);

              response.SetResponseUri(builder.Uri);
            }
              }

              return response;
        }
        private ImapWebResponse GetCreateResponse()
        {
            CloseMailbox(); // ignore error

              ImapMailbox createdMailbox;

              var response = new ImapWebResponse(Session.Create(RequestMailbox, out createdMailbox));

              if (response.Result.Failed)
            return response;

              if (Subscription) {
            Session.Subscribe(createdMailbox);
            Session.Lsub(createdMailbox);
              }
              else {
            Session.List(createdMailbox);
              }

              response.Mailboxes = new[] {createdMailbox};
              response.SetResponseUri(createdMailbox.Url);

              return response;
        }
        private ImapWebResponse GetRenameResponse()
        {
            CloseMailbox(); // ignore error

              ImapMailbox renamedMailbox;

              var mailbox = RequestMailbox;
              var response = new ImapWebResponse(Session.Rename(mailbox, GetDestinationMailbox(), out renamedMailbox));

              if (response.Result.Failed)
            return response;

              if (Subscription) {
            Session.Unsubscribe(mailbox);
            Session.Subscribe(renamedMailbox);
            Session.Lsub(renamedMailbox);
              }
              else {
            Session.List(renamedMailbox);
              }

              // unsubscribe/subscribe children
              if (Subscription && !string.IsNullOrEmpty(renamedMailbox.HierarchyDelimiter)) {
            var wildcard = renamedMailbox.HierarchyDelimiter + "*";
            ImapMailbox[] children;

            if (Session.Lsub(mailbox + wildcard, out children).Succeeded) {
              foreach (var child in children) {
            Session.Unsubscribe(child);
              }
            }

            if (Session.List(renamedMailbox.Name + wildcard, out children).Succeeded) {
              foreach (var child in children) {
            Session.Subscribe(child);
              }
            }
              }

              response.Mailboxes = new[] {renamedMailbox};
              response.SetResponseUri(renamedMailbox.Url);

              return response;
        }
        private ImapWebResponse GetAppendResponse()
        {
            if (beginAppendAsyncResult == null)
            throw new InvalidOperationException("GetRequestStream not called");

              try {
            appendMessageBodyStream.UpdateLength();

            ImapAppendedUidSet appendedUid;

            var response = new ImapWebResponse(Session.EndAppend(beginAppendAsyncResult, out appendedUid));

            if (response.Result.Succeeded) {
              // set empty stream; WebClient.Upload*() methods call WebResponse.GetResponseStream
              response.SetResponseStream(Stream.Null, 0L, false);

              if (appendedUid != null) {
            var builder = new ImapUriBuilder(RequestUri);

            builder.UidValidity = appendedUid.UidValidity;
            builder.Uid = appendedUid.ToNumber(); // if appendedUid.IsSingle

            response.SetResponseUri(builder.Uri);
              }
            }

            return response;
              }
              finally {
            appendMessageBodyStream.InternalDispose();
            appendMessageBodyStream = null;

            beginAppendAsyncResult = null;
              }
        }