void ClipboardExchange::onRequest(UINT32 reqCode, RfbInputGate *input)
{
  switch (reqCode) {
  case ClientMsgDefs::CLIENT_CUT_TEXT:
    input->readUInt8(); // pad
    input->readUInt16(); // pad
    {
      UINT32 length = input->readUInt32();
      std::vector<char> charBuff(length + 1);

      input->readFully(&charBuff.front(), length);
      charBuff[length] = '\0';
      AnsiStringStorage ansiText(&charBuff.front());

      if (!m_viewOnly) {
        StringStorage clipText;
        ansiText.toStringStorage(&clipText);
        m_desktop->setNewClipText(&clipText);
      }
    }
    break;
  default:
    StringStorage errMess;
    errMess.format(_T("Unknown %d protocol code received"), (int)reqCode);
    throw Exception(errMess.getString());
    break;
  }
}
	std::string& Clipboard::GetTextImpl()
	{
		static std::string clipboardText;
		HWND eventWindow = Win32Host::Win32Instance()->GetEventWindow();
		if (!OpenClipboard(eventWindow))
		{
			std::string error(Win32Utils::QuickFormatMessage(GetLastError()));
			throw ValueException::FromFormat("Could not open Win32 clipbaord: %s",
				error.c_str());
		}

		// TODO(mrobinson): We need to also test for text/html here, I think.
		UINT format = 0;
		clipboardText = "";
		if (IsClipboardFormatAvailable(CF_UNICODETEXT))
			format = CF_UNICODETEXT;
		else if (IsClipboardFormatAvailable(CF_TEXT))
			format = CF_TEXT;
		else
			return clipboardText;

		HANDLE clipboardData = GetClipboardData(format);
		if (!clipboardData)
		{
			CloseClipboard();
			std::string error(Win32Utils::QuickFormatMessage(GetLastError()));
			throw ValueException::FromFormat(
				"Could not get clipbaord data: %s", error.c_str());
		}

		LPVOID data = ::GlobalLock(clipboardData);
		if (!data)
		{
			CloseClipboard();
			std::string error(Win32Utils::QuickFormatMessage(GetLastError()));
			throw ValueException::FromFormat(
				"Could not lock clipbaord data: %s", error.c_str());
		}

		if (format == CF_TEXT)
		{
			std::string ansiText(static_cast<char*>(data));
			clipboardText = ::MultiByteToMultiByte(ansiText, CP_ACP, CP_UTF8);
		}
		else if (format == CF_UNICODETEXT)
		{
			std::wstring wideString(static_cast<wchar_t*>(data));
			clipboardText = ::WideToUTF8(wideString);
		}

		::GlobalUnlock(clipboardData);
		CloseClipboard();
		return clipboardText;
	}