public async Task <PreKeyBundle> getPreKey(TextSecureAddress destination, uint deviceId)// throws IOException
        {
            try
            {
                String path = string.Format(PREKEY_DEVICE_PATH, destination.getNumber(),
                                            deviceId.ToString());

                if (destination.getRelay().HasValue)
                {
                    path = path + "?relay=" + destination.getRelay().ForceGetValue();
                }

                String responseText = await makeRequest(path, "GET", null);

                PreKeyResponse response = JsonUtil.fromJson <PreKeyResponse>(responseText);

                if (response.getDevices() == null || response.getDevices().Count < 1)
                {
                    throw new Exception("Empty prekey list");
                }

                PreKeyResponseItem device                = response.getDevices()[0];
                ECPublicKey        preKey                = null;
                ECPublicKey        signedPreKey          = null;
                byte[]             signedPreKeySignature = null;
                int preKeyId       = -1;
                int signedPreKeyId = -1;

                if (device.getPreKey() != null)
                {
                    preKeyId = (int)device.getPreKey().getKeyId();// TODO: whacky
                    preKey   = device.getPreKey().getPublicKey();
                }

                if (device.getSignedPreKey() != null)
                {
                    signedPreKeyId        = (int)device.getSignedPreKey().getKeyId();// TODO: whacky
                    signedPreKey          = device.getSignedPreKey().getPublicKey();
                    signedPreKeySignature = device.getSignedPreKey().getSignature();
                }

                return(new PreKeyBundle(device.getRegistrationId(), device.getDeviceId(), (uint)preKeyId, preKey,
                                        (uint)signedPreKeyId, signedPreKey, signedPreKeySignature, response.getIdentityKey()));
            }

            /*catch (JsonUtil.JsonParseException e)
             * {
             *  throw new IOException(e);
             * }*/
            catch (NotFoundException nfe)
            {
                throw new UnregisteredUserException(destination.getNumber(), nfe);
            }
        }
 /**
  * Send a delivery receipt for a received message.  It is not necessary to call this
  * when receiving messages through {@link org.whispersystems.textsecure.api.TextSecureMessagePipe}.
  * @param recipient The sender of the received message you're acknowledging.
  * @param messageId The message id of the received message you're acknowledging.
  * @throws IOException
  */
 public void sendDeliveryReceipt(TextSecureAddress recipient, ulong messageId)
 {
     this.socket.sendReceipt(recipient.getNumber(), messageId, recipient.getRelay());
 }
        private OutgoingPushMessageList getEncryptedMessages(PushServiceSocket socket,
                                                   TextSecureAddress recipient,
                                                   ulong timestamp,
                                                   byte[] plaintext,
                                                   bool legacy)
        {
            List<OutgoingPushMessage> messages = new List<OutgoingPushMessage>();

            if (!recipient.Equals(localAddress))
            {
                messages.Add(getEncryptedMessage(socket, recipient, TextSecureAddress.DEFAULT_DEVICE_ID, plaintext, legacy));
            }

            foreach (uint deviceId in store.GetSubDeviceSessions(recipient.getNumber()))
            {
                messages.Add(getEncryptedMessage(socket, recipient, deviceId, plaintext, legacy));
            }

            return new OutgoingPushMessageList(recipient.getNumber(), timestamp, recipient.getRelay().HasValue ? recipient.getRelay().ForceGetValue() : null, messages);
        }
        public async Task <List <PreKeyBundle> > getPreKeys(TextSecureAddress destination, uint deviceIdInteger)// throws IOException
        {
            try
            {
                String deviceId = deviceIdInteger.ToString();

                if (deviceId.Equals("1"))
                {
                    deviceId = "*";
                }

                String path = String.Format(PREKEY_DEVICE_PATH, destination.getNumber(), deviceId);

                if (destination.getRelay().HasValue)
                {
                    path = path + "?relay=" + destination.getRelay().ForceGetValue();
                }

                String responseText = await makeRequest(path, "GET", null);

                PreKeyResponse      response = JsonUtil.fromJson <PreKeyResponse>(responseText);
                List <PreKeyBundle> bundles  = new List <PreKeyBundle>();

                foreach (PreKeyResponseItem device in response.getDevices())
                {
                    ECPublicKey preKey                = null;
                    ECPublicKey signedPreKey          = null;
                    byte[]      signedPreKeySignature = null;
                    int         preKeyId              = -1;
                    int         signedPreKeyId        = -1;

                    if (device.getSignedPreKey() != null)
                    {
                        signedPreKey          = device.getSignedPreKey().getPublicKey();
                        signedPreKeyId        = (int)device.getSignedPreKey().getKeyId(); // TODO: whacky
                        signedPreKeySignature = device.getSignedPreKey().getSignature();
                    }

                    if (device.getPreKey() != null)
                    {
                        preKeyId = (int)device.getPreKey().getKeyId();// TODO: whacky
                        preKey   = device.getPreKey().getPublicKey();
                    }

                    bundles.Add(new PreKeyBundle(device.getRegistrationId(), device.getDeviceId(), (uint)preKeyId,
                                                 preKey, (uint)signedPreKeyId, signedPreKey, signedPreKeySignature,
                                                 response.getIdentityKey()));        // TODO: whacky
                }

                return(bundles);
            }

            /*catch (JsonUtil.JsonParseException e)
             * {
             *  throw new IOException(e);
             * }*/
            catch (NotFoundException nfe)
            {
                throw new UnregisteredUserException(destination.getNumber(), nfe);
            }
        }
        public async Task<PreKeyBundle> getPreKey(TextSecureAddress destination, uint deviceId)// throws IOException
        {
            try
            {
                String path = string.Format(PREKEY_DEVICE_PATH, destination.getNumber(),
                                            deviceId.ToString());

                if (destination.getRelay().HasValue)
                {
                    path = path + "?relay=" + destination.getRelay().ForceGetValue();
                }

                String responseText = await makeRequest(path, "GET", null);
                PreKeyResponse response = JsonUtil.fromJson<PreKeyResponse>(responseText);

                if (response.getDevices() == null || response.getDevices().Count < 1)
                    throw new Exception("Empty prekey list");

                PreKeyResponseItem device = response.getDevices()[0];
                ECPublicKey preKey = null;
                ECPublicKey signedPreKey = null;
                byte[] signedPreKeySignature = null;
                int preKeyId = -1;
                int signedPreKeyId = -1;

                if (device.getPreKey() != null)
                {
                    preKeyId = (int)device.getPreKey().getKeyId();// TODO: whacky
                    preKey = device.getPreKey().getPublicKey();
                }

                if (device.getSignedPreKey() != null)
                {
                    signedPreKeyId = (int)device.getSignedPreKey().getKeyId();// TODO: whacky
                    signedPreKey = device.getSignedPreKey().getPublicKey();
                    signedPreKeySignature = device.getSignedPreKey().getSignature();
                }

                return new PreKeyBundle(device.getRegistrationId(), device.getDeviceId(), (uint)preKeyId, preKey,
                                        (uint)signedPreKeyId, signedPreKey, signedPreKeySignature, response.getIdentityKey());
            }
            /*catch (JsonUtil.JsonParseException e)
            {
                throw new IOException(e);
            }*/
            catch (NotFoundException nfe)
            {
                throw new UnregisteredUserException(destination.getNumber(), nfe);
            }
        }
        public async Task<List<PreKeyBundle>> getPreKeys(TextSecureAddress destination, uint deviceIdInteger)// throws IOException
        {
            try
            {
                String deviceId = deviceIdInteger.ToString();

                if (deviceId.Equals("1"))
                    deviceId = "*";

                String path = String.Format(PREKEY_DEVICE_PATH, destination.getNumber(), deviceId);

                if (destination.getRelay().HasValue)
                {
                    path = path + "?relay=" + destination.getRelay().ForceGetValue();
                }

                String responseText = await makeRequest(path, "GET", null);
                PreKeyResponse response = JsonUtil.fromJson<PreKeyResponse>(responseText);
                List<PreKeyBundle> bundles = new List<PreKeyBundle>();

                foreach (PreKeyResponseItem device in response.getDevices())
                {
                    ECPublicKey preKey = null;
                    ECPublicKey signedPreKey = null;
                    byte[] signedPreKeySignature = null;
                    int preKeyId = -1;
                    int signedPreKeyId = -1;

                    if (device.getSignedPreKey() != null)
                    {
                        signedPreKey = device.getSignedPreKey().getPublicKey();
                        signedPreKeyId = (int)device.getSignedPreKey().getKeyId(); // TODO: whacky
                        signedPreKeySignature = device.getSignedPreKey().getSignature();
                    }

                    if (device.getPreKey() != null)
                    {
                        preKeyId = (int)device.getPreKey().getKeyId();// TODO: whacky
                        preKey = device.getPreKey().getPublicKey();
                    }

                    bundles.Add(new PreKeyBundle(device.getRegistrationId(), device.getDeviceId(), (uint)preKeyId,
                                                         preKey, (uint)signedPreKeyId, signedPreKey, signedPreKeySignature,
                                                         response.getIdentityKey()));// TODO: whacky
                }

                return bundles;
            }
            /*catch (JsonUtil.JsonParseException e)
            {
                throw new IOException(e);
            }*/
            catch (NotFoundException nfe)
            {
                throw new UnregisteredUserException(destination.getNumber(), nfe);
            }
        }