SOLVEDRequest some help; Adding TCP server/client to webserver not smooth/workab

I have 2 arduino mega with EthernetShield 5100, they both run a webserver for over a year now without any problems.
Recently I added a TCP client and server on both so to communicate with each other (sending a 0 or 1 to turn on and off a led)
It’s all doing what it has to but regularly I lose the webserver connection, not be able to reach the webpage, 5 minutes later everything is ok again, also sometimes the TCP connection takes up to a minute to react.
Don’t know what’s happening but seems to be hanging sometimes in void loop TCPClient(or server) and sometimes in void loop WEBServer (just my simple way to try to explain what could be happening)
And sometimes it just works perfectly without any delay…

Found the original TCP code I used here

I use different ports for the webserver and the TCP connection and use "3 different loops"

Part of my code

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE1};
byte ip[] = {192, 168, 0, 81 };
EthernetServer server(184);
const int TCPserverPort = 185;
IPAddress serverAddress(192, 168, 0, 80);
EthernetClient TCPclient;
EthernetServer TCPserver(TCPserverPort);
Void setup

  //start Ethernet and server
  Ethernet.begin(mac, ip);
  server.begin();
  if (Ethernet.begin(mac) == 0)
    Serial.println("Failed to configure Ethernet using DHCP");

  // Listening for a TCP client (from Arduino #1)
  TCPserver.begin();

  // connect to TCP server (Arduino #2)
  if (TCPclient.connect(serverAddress, TCPserverPort))
    Serial.println("Connected to TCP server");
  else
    Serial.println("Failed to connect to TCP server");
void loop()
{
  TCPClient();
  TCPServer();
  WEBServer();

}

void TCPClient() {

  if (!TCPclient.connected()) {
    //  Serial.println("Connection is disconnected");
    TCPclient.stop();

    // reconnect to TCP server (Arduino #2)
    if (TCPclient.connect(serverAddress, TCPserverPort))
      //   Serial.println("Reconnected to TCP server");
      Serial.println();
    else
      //    Serial.println("Failed to reconnect to TCP server");
      Serial.println();
  }

  //VenusR2
  if (digitalRead(VenusR2) == HIGH) {

    TCPclient.write('0');
    TCPclient.flush();
    //   Serial.println("- The button is pressed,  sent command: 0");
  }
  else if (digitalRead(VenusR2) == LOW) {
    //
    TCPclient.write('1');
    TCPclient.flush();
    //   Serial.println("- The button is pressed,  sent command: 1");
  }
void TCPServer() {
  // Wait for a TCP client from Arduino #1:
  EthernetClient client1 = TCPserver.available();

  if (client1) {
    // Read the command from the TCP client:
    char command = client1.read();
    //   Serial.print("- Received command: ");
    //   Serial.println(command);

    if (command == '1')
      digitalWrite(LED_PIN, HIGH); // Turn LED on
    else if (command == '0')
      digitalWrite(LED_PIN, LOW);  // Turn LED off

    Ethernet.maintain();

  }
}

What can I try to do to get this thing smooth/workable?
The reason to add this TCP is only a way to get a led on or off on 1 Arduino by making an input high or low on the other, maybe TCP is not the right way?

TIA
Dirk

So did some trial and error, turns out when I leave this part out of the sketch (TCPclient part)

 if (digitalRead(VenusR2) == HIGH) {

    TCPclient.write('0');
    TCPclient.flush();
    //   Serial.println("- The button is pressed,  sent command: 0");
  }
  else if (digitalRead(VenusR2) == LOW) {
    //
    TCPclient.write('1');
    TCPclient.flush();
    //   Serial.println("- The button is pressed,  sent command: 1");
  }

The webserver as well as the TCPServer is responding ok, without delay
Which I think means that as soon as the TCPClient is sending (in this case) 0 or 1 everything else slows down/has to wait.
Is it the code, is it the way TCP works or…?
Also in this code TCP is constantly repeating sending a 0 or 1, maybe it’s a bit overdone.

Please give me a hint in the right direction :slight_smile:

So I added

 TCPclient.stop();

at the end of void TCPClient and everything seems ok