Photon using Wifi on non internet connected network

I am using a photon to control a motor. This is connected to a raspberry pi via WiFi and telnet connection.
Everything works beautifully when the photon is cloud connected. But I have a situation where this needs to be used in a non internet connected network. When not able to connect to the cloud, the photon code is not running. Reading the docs I think I need to do the following:

SYSTEM_MODE(MANUAL);
TCPServer server = TCPServer(23);
TCPClient client;
void setup() {

  WiFi.onO;
  WiFi.connect();
  //code to connect the TCPServer and client etc....
}

void loop() {
 if (client.connected()) {
    // echo all available bytes back to the client
    while (client.available()) {
      buffer[bytecount] = client.read();
      bytecount++;
    }
    ... code to process client message 
    } 
}

When I do this, the telnet connection connects but no message is processed. I have to put the photon in safe mode to recover. I also tried this:

SYSTEM_MODE(MANUAL);
TCPServer server = TCPServer(23);
TCPClient client;
void setup() {

  WiFi.onO;
  WiFi.connect();
 }

void loop() {
       Particle.process();
 if (client.connected()) {
    // echo all available bytes back to the client
    while (client.available()) {
      buffer[bytecount] = client.read();
      bytecount++;
    }
    ... code to process client message 
    } 
}

same results Telnet connects but no message processing.
If I comment out the SYSTEM_MODE statement. Everything works as expected. What am I missing?

Thanks for you help.

Further comment. I stated at the bottom of the message that everything works as expected. I meant to say that everything as expected when connected to the internet/cloud. When not connected to the cloud there is no message processing. Same as when I use SYSTEM_MODE(MANUAL)

should be WiFi.on();

You may also want to add a check if (WiFi.ready()) or waitUntil(WiFi.ready) and call Particle.process() (or check for a valid WiFi.localIP()) before you do anything with the client.

In AUTOMATIC all this is ensured to be set up correctly before your code runs, in non-AUTOMATIC this is your own responsibility.

I made a new post with my example code of how I got an example telnet server working without Wi-Fi in manual mode.