class IcapClient implements Closeable {

  public IcapClient connect() throws IcapException {
    if (finalResult != null) {
      finalResult.clear();
      finalResult = null;
    }
    if (client != null) {
      close();
    }
    logger
        .debug("Try connect to {}:{} service {}", serverIP, port, icapService);
    try {
      // Initialize connection
      client = new Socket(serverIP, port);
      client.setReuseAddress(true);
      client.setKeepAlive(true);
      client.setSoTimeout(timeout);
      client.setTcpNoDelay(false);
      // Opening out stream
      out = client.getOutputStream();
      // Opening in stream
      in = client.getInputStream();
      if (setPreviewSize < 0) {
        getFromServerPreviewSize();
      }
      logger.debug("Connected with Preview Size = {}", stdPreviewSize);
      return this;
    } catch (final SocketTimeoutException e) {
      close();
      logger.error(TIMEOUT_OCCURS_WITH_THE_SERVER_SINCE, serverIP, port,
                   e.getMessage());
      throw new IcapException(TIMEOUT_OCCURS_WITH_THE_SERVER, e,
                              IcapError.ICAP_TIMEOUT_ERROR);
    } catch (final ConnectException e) {
      close();
      logger.error("Could not connect to server {}:{} since {}", serverIP, port,
                   e.getMessage());
      throw new IcapException("Could not connect with the server", e,
                              IcapError.ICAP_CANT_CONNECT);
    } catch (final IOException e) {
      close();
      logger.error("Could not connect to server {}:{} since {}", serverIP, port,
                   e.getMessage());
      throw new IcapException("Could not connect with the server", e,
                              IcapError.ICAP_NETWORK_ERROR);
    } catch (final IcapException e) {
      close();
      throw e;
    }
  }

  public  IcapClient(final String serverIP, final int port,
                    final String icapService);
  public  IcapClient(final String serverIP, final int port,
                    final String icapService, final int previewSize);

}