In the instructions, do something, for example
if tcp.Connected then begin Memo1.Lines.Add('Ура порт открыт'); end;
and it will work out the next instruction in any cases, regardless of whether the port is open or not
tcp.Close; //Закрываем порт или соединение
You can try in the try except block.
ps type in the search engines "port scanner delphi 7", the options are darkness.
Regarding the scanning speed, simple ways to do it will not work.
TCP session setup
The client sends a TCP segment with a SYN bit (flag) and a 32-bit filled field "Sequence" with port numbers DST (Destination Port) and SRC (Source Port), the server adds "1" to the value of the "Sequence Number" field and fills the field "Confirmation number", sets the ASK bit and sends to the client, the client removes the SYN bit, adds "1" to the "Confirmation number" field and sends it to the server, the entire TCP session is established.
If it is simpler, it happens like "client -> server", "server -> client", "client-> server".
Closing the active connection is the same, only with the FIN bit.
Delphi components work according to the above "scheme". Avoid 3 stages (client-> server) can Nmap scanner. Nmap determines whether the port is open or not without full connection only on the analysis of the second packet with SYN and ASK.