Beware that connecting UNO with ESP, if conections are TX/TX and RX/RX or TX/RX and RX/TX, both are different scenarios : one is connecting the ESP to USB while the other is connecting ESP to ATMega328, you can NOT have both at the same time.
About the code I've provided, I'm using it for Telnet Server on ESP since almost 18 months, so it has been tested a lot