What I get is that the socket (I'll always refer to StreamPeerTCP instances as "socket" in this question) starts in STATUS_NONE. Then when you establish a connection it will go through STATUS_CONNECTING and STATUS_CONNECTED. Then, on closed, it will go back to STATUS_NONE again.

But there is another status which is STATUS_ERROR. I have several questions out of it:

  1. What are all the scenarios (actually: scenario types) where a socket can go into STATUS_ERROR status?
  2. Is that a recoverable or an unrecoverable status? What should I understand when reaching the STATUS_ERROR status? Should I close the connection from my side and start over again? Or are there scenarios where I can keep my connection and keep it open / not consider it closed / terminated / ... even when on STATUS_ERROR?

I'm asking because I found some scenarios when I had an async loop that waited until a socket.poll(), then I found that the status was STATUS_ERROR suddenly (and I was working my look by always requiring the current status to be STATUS_CONNECTED actually).

Wairing for the .poll was done using Godot 4's await keyword (i.e. await self.custom_signal) considering a _process like this:

func _process(d):
    socket.poll()
    emit_signal("custom_signal")

Ok, I'll add more context. This component works against a server in the same network, which is my domestic WiFi network -- discard the possibility of any firewall blocking my connection or related stuffs, and also I'm not attempting any internet (i.e. out of my network) connection. The loop looks like this:

# Please assume all the signals I use here, exist.
var _stream : StreamPeerTCP = StreamPeerTCP.new()

func _wait_socket_status(statuses : Array):
	"""
	Waits until the status is in a certain set of values.
	
	This is a co-routine that must be waited for.
	"""
	
	if len(statuses) == 0:
		return
	
	while true:
		await self.after_poll
		if _stream.get_status() in statuses:
			break

func _data_arrival_loop():
	"""
	Performs a data arrival loop to read data from this
	socket. This also processes the response accordingly.

	This is a co-routine that must NOT be waited for.
	It must be invoked when the session is starting.
	"""

	emit_signal("debug_data_loop_started")
	await self.after_poll
	while _stream.get_status() == StreamPeerTCP.STATUS_CONNECTED:
		if _stream.get_available_bytes() > 0:
			var response = _stream.get_data(_stream.get_available_bytes())
			_my_method_to_process_the_response(response[1])
		await self.after_poll
	emit_signal("debug_data_loop_ended")

func connect(host):
	var error = _stream.connect_to_host(host, 2357)
	if error != OK:
		emit_signal("session_failed", error)
	else:
		emit_signal("session_starting")
		await _wait_socket_status(StreamPeerTCP.STATUS_CONNECTED)
		# And then I launch the loop asynchronously:
		_data_arrival_loop()

func _process(d):
	socket.poll()
	emit_signal("after_poll")

The problem is that suddenly the socket enters in error status, and I don't get why is that error status at all, also. I'd like to understand better to whether that error status is recoverable or should I start over, and stuff like that.