Repository: node
Issue: Http2Session emits 'connect' event while socket is still connecting
Labels: http2

### Version

Tested with v20.7.0 and v18.17.1

### Platform

Probably platform independent. Darwin MacBook-Pro-M1-Max.local 22.6.0 Darwin Kernel Version 22.6.0: Wed Jul  5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000 arm64

### Subsystem

http2

### What steps will reproduce the bug?

When `createConnection` returns a socket that has `connecting === true` but no `_handle` set, the Http2Session constructor will wrap the socket with a `JSStreamSocket` and this wrapper will not reflect that the underlying socket is still connecting. The session will then emit a 'connect' event while the socket is still connecting.

Code demonstrating the issue when connecting to a port that is not being listened on:
```js
const net = require('net');
const http2 = require('http2');

const session = http2.connect('http://[::1]:12345', {
	createConnection: () => {
		const socket = new net.Socket();
		socket.connecting = true;
		// socket._handle = { isStreamBase: true }; // Uncomment to confirm _handle makes a difference.
		socket.on('error', err => {
			console.error('socket:', err);
		});
		socket.on('connect', () => {
			console.log('socket: connect');
		});
		socket.on('close', () => {
			console.log('socket: close');
		});
		
		setTimeout(() => {
			// socket._handle = undefined; // Uncomment to confirm _handle makes a difference.
			socket.connect(12345, '::1');
		}, 100);

		return socket;
	}
});

session.on('error', err => {
	console.error('http2 session:', err);
});
session.on('connect', () => {
	console.log('http2 session: connect');
});
session.on('close', () => {
	console.log('http2 session: close');
});
```

The output:
```
http2 session: connect
socket: Error: connect ECONNREFUSED ::1:12345
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 12345
}
socket: close
http2 session: Error: connect ECONNREFUSED ::1:12345
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 12345
}
http2 session: close
```

The 'connect' event is unexpected.

Setting the `_handle` as the commented 2 lines of code suggest can trick the constructor of Http2Session into not wrapping the socket and wait for the socket to emit a 'connect' event before emitting 'connect' itself. This is provided only illustration, setting `_handle` this way is not expected to be a valid workaround.

Code pointers:
- Http2Sesssion wraps the socket in a JSStreamSocket because the `_handle` is undefined: https://github.com/nodejs/node/blob/a94966ffc114e831d4a917a97f9958d3385ab6f7/lib/internal/http2/core.js#L1208
- The wrapper does not have `connecting === true` and the check of Http2Session whether the socket (now the wrapper) is still connecting does not trigger: https://github.com/nodejs/node/blob/a94966ffc114e831d4a917a97f9958d3385ab6f7/lib/internal/http2/core.js#L1256


### How often does it reproduce? Is there a required condition?

Always reproduces with the above sample code.

### What is the expected behavior? Why is that the expected behavior?

Http2Session should only emit a 'connect' event once the underlying socket is connected.

### What do you see instead?

Http2Session emits 'connect' event while socket is still connecting.

### Additional information

_No response_