Repository: axios
Issue: AxiosError: Client network socket disconnected before secure TLS connection was established
Labels: priority::low

### Describe the issue

I have a AWS lambda function that makes a post request to fetch access token through client_credentials flow. The axios throws this error randomly but not everytime.

AxiosError: Client network socket disconnected before secure TLS connection was established
    at Function.AxiosError.from (/var/task/index.js:434:1017576)
    at RedirectableRequest.handleRequestError (/var/task/index.js:434:1039936)
    at RedirectableRequest.emit (node:events:513:28)
    at ClientRequest.b.forEach.m.<computed> (/var/task/index.js:390:142890)
    at ClientRequest.emit (node:events:513:28)
    at TLSSocket.socketErrorListener (node:_http_client:494:9)
    at TLSSocket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  localAddress: undefined,
  port: 443,
  host: '<keycloak-url>',
  path: null,
  code: 'ECONNRESET',
  config: {
    transitional: {
      silentJSONParsing: true,
      forcedJSONParsing: true,
      clarifyTimeoutError: false
    },
    adapter: [ 'xhr', 'http' ],
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 0,
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    env: { FormData: [Function], Blob: null },
    validateStatus: [Function: validateStatus],
    headers: AxiosHeaders {
      Accept: 'application/json, text/plain, */*',
      'Content-Type': 'application/x-www-form-urlencoded',
      'Accept-Encoding': 'application/json',
      'User-Agent': 'axios/1.2.1',
      'Content-Length': '29'
    },
    httpsAgent: Agent {
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      defaultPort: 443,
      protocol: 'https:',
      options: [Object: null prototype],
      requests: [Object: null prototype] {},
      sockets: [Object: null prototype],
      freeSockets: [Object: null prototype] {},
      keepAliveMsecs: 1000,
      keepAlive: false,
      maxSockets: Infinity,
      maxFreeSockets: 256,
      scheduling: 'lifo',
      maxTotalSockets: Infinity,
      totalSocketCount: 4,
      maxCachedSessions: 100,
      _sessionCache: [Object],
      [Symbol(kCapture)]: false
    },
    auth: {
      username: xxxxxxxxx,
      password: xxxxxxxxx
    },
    method: 'post',
    url: '<keycloak-realm-url>/protocol/openid-connect/token',
    data: 'grant_type=client_credentials'
  },
  request: <ref *1> Writable {
    _writableState: WritableState {
      objectMode: false,
      highWaterMark: 16384,
      finalCalled: false,
      needDrain: false,
      ending: false,
      ended: false,
      finished: false,
      destroyed: false,
      decodeStrings: true,
      defaultEncoding: 'utf8',
      length: 0,
      writing: false,
      corked: 0,
      sync: true,
      bufferProcessing: false,
      onwrite: [Function: bound onwrite],
      writecb: null,
      writelen: 0,
      afterWriteTickInfo: null,
      buffered: [],
      bufferedIndex: 0,
      allBuffers: true,
      allNoop: true,
      pendingcb: 0,
      constructed: true,
      prefinished: false,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: true,
      errored: null,
      closed: false,
      closeEmitted: false,
      [Symbol(kOnFinished)]: []
    },
    _events: [Object: null prototype] {
      response: [Function: handleResponse],
      error: [Function: handleRequestError],
      socket: [Function: handleRequestSocket]
    },
    _eventsCount: 3,
    _maxListeners: undefined,
    _options: {
      maxRedirects: 21,
      maxBodyLength: Infinity,
      protocol: 'https:',
      path: 'xxxxxxxxx/protocol/openid-connect/token',
      method: 'POST',
      headers: [Object: null prototype],
      agents: [Object],
      auth: 'xxxxxxxxxxxxxxxxxxx',
      beforeRedirect: [Function: dispatchBeforeRedirect],
      beforeRedirects: [Object],
      hostname: '<keycloak-hostname>',
      port: '',
      agent: [Agent],
      nativeProtocols: [Object],
      pathname: '/xxxxxxxxx/protocol/openid-connect/token'
    },
    _ended: false,
    _ending: true,
    _redirectCount: 0,
    _redirects: [],
    _requestBodyLength: 29,
    _requestBodyBuffers: [ [Object] ],
    _onNativeResponse: [Function (anonymous)],
    _currentRequest: ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: false,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      maxRequestsOnConnectionReached: false,
      _defaultKeepAlive: true,
      useChunkedEncodingByDefault: true,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      strictContentLength: false,
      _contentLength: '29',
      _hasBody: true,
      _trailer: '',
      finished: false,
      _headerSent: true,
      _closed: false,
      socket: [TLSSocket],
      _header: 'POST /xxxxxxxxxxx/protocol/openid-connect/token HTTP/1.1\r\n' +
        'Accept: application/json, text/plain, */*\r\n' +
        'Content-Type: application/x-www-form-urlencoded\r\n' +
        'Accept-Encoding: application/json\r\n' +
        'User-Agent: axios/1.2.1\r\n' +
        'Content-Length: 29\r\n' +
        'Host: <keycloak-hostname>\r\n' +
        'Authorization: Basic xxxxxxxxxxxxxxx\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: nop],
      agent: [Agent],
      socketPath: undefined,
      method: 'POST',
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      path: 'xxxxxxx/protocol/openid-connect/token',
      _ended: false,
      res: null,
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      host: '<keycloak-url>',
      protocol: 'https:',
      _redirectable: [Circular *1],
      [Symbol(kCapture)]: false,
      [Symbol(kBytesWritten)]: 0,
      [Symbol(kEndCalled)]: false,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype],
      [Symbol(kUniqueHeaders)]: null
    },
    _currentUrl: 'xxxxxxxxxxxxxx/protocol/openid-connect/token',
    [Symbol(kCapture)]: false
  },
  cause: Error: Client network socket disconnected before secure TLS connection was established
      at connResetException (node:internal/errors:705:14)
      at TLSSocket.onConnectEnd (node:_tls_wrap:1594:19)
      at TLSSocket.emit (node:events:525:35)
      at endReadableNT (node:internal/streams/readable:1358:12)
      at processTicksAndRejections (node:internal/process/task_queues:83:21) {
    code: 'ECONNRESET',
    path: null,
    host: '<keycloak-url>',
    port: 443,
    localAddress: undefined
  }
}

### Example Code

_No response_

### Expected behavior

Expected to fetch the access token seamlessly and I do see this behaviour when I make the same post call through postman

### Axios Version

1.2.1

### Adapter Version

_No response_

### Browser

_No response_

### Browser Version

_No response_

### Node.js Version

v16.14.0

### OS

_No response_

### Additional Library Versions

_No response_

### Additional context/Screenshots

_No response_