private void writeToSocket() throws IOException {
    if (socketFactory == null) return;

    if (sock != null) {
      try {
        doWriteToSocket();
        return;
      } catch (IOException e) {
        close();
        retryConnectAt = 0;
      }
    }

    if (retryConnectAt > 0 && retryConnectAt > System.currentTimeMillis()) {
      return;
    }

    try {
      connect();
    } catch (IOException e) {
      retryConnectAt = System.currentTimeMillis() + tcpRetryInterval;
      throw e;
    }
    doWriteToSocket();
  }