How to use ESP8266/ESP32 as a TCP server and client with sockets

How to use ESP8266/ESP32 as a TCP server and client with sockets