void ClipboardExchange::execute()
{
  while (!isTerminating()) {
    m_newClipWaiter.waitForEvent();

    if (m_hasNewClip && !isTerminating() && !m_viewOnly) {

      try {
        AutoLock al(m_output);
        m_output->writeUInt8(ServerMsgDefs::SERVER_CUT_TEXT); // type
        m_output->writeUInt8(0); // pad
        m_output->writeUInt16(0); // pad

        AnsiStringStorage charBuff;
        {
          AutoLock al(&m_storedClipMut);
          charBuff.fromStringStorage(&m_storedClip);
          m_hasNewClip = false;
        }
        size_t strLength = charBuff.getLength();
        m_output->writeUInt32((UINT32)strLength);
        m_output->writeFully(charBuff.getString(), strLength);

        m_output->flush();
      } catch (Exception &e) {
        m_log->error(_T("The clipboard thread force to terminate because")
                   _T(" it caught the error: %s"), e.getMessage());
        terminate();
      }
    }
  }
}