private boolean ensureConnected() {
    if (pipeHandle != WinNT.INVALID_HANDLE_VALUE) return true;

    int retryCount = 5;
    while (pipeHandle == WinNT.INVALID_HANDLE_VALUE) {
      pipeHandle =
          kernel32.CreateFile(
              "\\\\.\\pipe\\pmb.quik.pipe",
              Kernel32.GENERIC_READ | Kernel32.GENERIC_WRITE,
              0,
              null,
              Kernel32.OPEN_EXISTING,
              0,
              null);
      int errorCode = kernel32.GetLastError();
      if (errorCode == 2 || errorCode == 231) { // трубы нет или занята
        forceDisconnect();
        if (--retryCount < 0) {
          //					System.out.println(errorCode);
          return false;
        }
      }
    }
    // System.out.println("Готово!");
    //		System.out.println(kernel32.GetLastError());
    if (kernel32.GetLastError() != Kernel32.ERROR_SUCCESS) {
      forceDisconnect();
      return false;
    }
    return true;
  }
  public String executeRequest(String command, boolean doCloseConnection) {
    try {
      // System.out.println("Соединяемся");
      if (ensureConnected()) {
        // String command = "stm";//"isc";//"staEQBREMU:SBER03";
        WinBase.OVERLAPPED overlapped = new WinBase.OVERLAPPED();
        ByteBuffer commandBytes = ByteBuffer.allocate(command.length() + 1);
        commandBytes.put(command.getBytes()).put((byte) 0);
        kernel32.WriteFile(
            pipeHandle,
            commandBytes.array(),
            commandBytes.capacity(),
            new IntByReference(),
            overlapped);
        // System.out.println(kernel32.GetLastError());
        kernel32.FlushFileBuffers(pipeHandle);
        // System.out.println(kernel32.GetLastError());
        while (overlapped.Internal.intValue() == WinNT.ERROR_IO_PENDING) ;

        // System.out.println("Записали");
        ByteBuffer buffer = ByteBuffer.allocate(4 * 1024);
        IntByReference bytesRead = new IntByReference(buffer.capacity());
        int lastError = 0;
        // System.out.println("Начали читать..");

        // проверим, чтобы не зависнуть
        if (kernel32.PeekNamedPipe(pipeHandle, buffer, buffer.capacity(), bytesRead, null, null))
          while (!(kernel32.ReadFile(pipeHandle, buffer, buffer.capacity(), bytesRead, overlapped))
              || (lastError = kernel32.GetLastError()) == Kernel32.ERROR_MORE_DATA) {
            // читаем и читаем
            if (lastError == Kernel32.ERROR_PIPE_NOT_CONNECTED
                || overlapped.Internal.intValue() != WinNT.ERROR_IO_PENDING) break;
          }

        // System.out.println("Считали: " + bytesRead.getValue() + " байт");
        if (doCloseConnection) {
          forceDisconnect();
        }
        String result = new String(buffer.array(), 0, bytesRead.getValue());
        // System.out.println("Quik pipe -> : " + result);

        if ("not connected".equals(result)) return null;
        return result;
      } else {
        // System.out.println("Quik Pipe cоединение не может быть установлено. Вероятно сервер
        // выключен");
        return null;
      }
    } finally {
      if (doCloseConnection) {
        forceDisconnect();
      }
    }
  }
 private void forceDisconnect() {
   if (pipeHandle != WinNT.INVALID_HANDLE_VALUE) {
     kernel32.CloseHandle(pipeHandle);
     pipeHandle = WinNT.INVALID_HANDLE_VALUE;
     // System.out.println("Закрыли трубу!");
   }
 }