public void sendMessage(Friend friend, StreamMessage msg, bool add_to_pending_messages, bool send_to_server, bool send_push_notification, bool remove_after_sending = false)
        {
            OffloadedMessage om = new OffloadedMessage {
                friend = friend, msg = msg, addToPendingMessages = add_to_pending_messages, sendToServer = send_to_server, sendPushNotification = send_push_notification, removeAfterSending = remove_after_sending
            };

            msgQueue.Add(om);
        }
        private void offloadedMessageProcessorLoop()
        {
            while (running)
            {
                while (msgQueue.Count > 0)
                {
                    try
                    {
                        OffloadedMessage om = msgQueue[0];
                        msgQueue.RemoveAt(0);
                        sendMessage(om);
                    }
                    catch (Exception e)
                    {
                        Logging.error("Unknown exception occured in offloadedMessageProcessorLoop: " + e);
                    }
                }

                Thread.Sleep(10); // TODO increase sleep onSleep, reset it onResume
            }
        }
        private void sendMessage(OffloadedMessage om)
        {
            PendingMessage       pm             = new PendingMessage(om.msg, om.sendToServer, om.sendPushNotification, om.removeAfterSending);
            StreamMessage        msg            = pm.streamMessage;
            PendingMessageHeader tmp_msg_header = tmp_msg_header = getPendingMessageHeader(om.friend, msg.id);

            if (tmp_msg_header != null)
            {
                pm.filePath = tmp_msg_header.filePath;
            }
            PendingRecipient tmp_recipient = null;

            lock (pendingRecipients)
            {
                tmp_recipient = pendingRecipients.Find(x => x.address.SequenceEqual(msg.recipient));
                if (tmp_recipient == null)
                {
                    tmp_recipient = new PendingRecipient(msg.recipient);
                    pendingRecipients.Add(tmp_recipient);
                }
            }
            if (om.addToPendingMessages)
            {
                pm.save(storagePath);
                if (tmp_msg_header == null)
                {
                    tmp_recipient.messageQueue.Add(new PendingMessageHeader()
                    {
                        filePath = pm.filePath, id = pm.streamMessage.id, sendToServer = pm.sendToServer
                    });
                }
            }
            if (tmp_recipient.messageQueue.Count == 1 || !om.addToPendingMessages)
            {
                sendMessage(om.friend, pm, om.addToPendingMessages);
            }
        }