Code on events is not executed always how to fix?

i have some code inside void oscEvent(OscMessage theOscMessage) event that resizes sketch size with surface
eg.

        pg.beginDraw();
        pg.background(0, 0, 0, 0);
        pg.endDraw();
        oldW = int(displayHeight*hostH/hostW);
        oldH = displayHeight;
        surface.setResizable(true);
        surface.setSize(int(displayHeight*hostH/hostW), displayHeight);
        surface.setLocation(100, 0);
        newAspectRatioY = displayHeight/hostW; //float(width)/float(hostH);

more or less you have to send 2 times the osc message so to trigger the code correctly.
How can i fix this?

is it a good idea to use osc messages for a remote drawing application like mouse cursors in ketai, or is it better and more stable to use tcp ip client/server?

/* using TCP with netP5 example */
import android.content.Intent;
import android.os.Bundle;

import ketai.net.bluetooth.*;
import ketai.ui.*;
import ketai.net.*;
import netP5.*;

TcpClient tcpc;
TcpServer tcps;

void setup() {
  
  /* lets create a TCP server and a TCP client, 
   * both will be talking to each other.
   * First we create a listener for the server and the client.
   * A listener will receive NetMessages which contain our data.
   * NetListener is an interface and requires to implement netEvent 
   * and netStatus. 
  */
  
  NetListener nl1 = new NetListener() {
    public void netEvent(NetMessage m) {
      println("netEvent (tcps) : "+ new String( m.getData() ) );
    }
    public void netStatus(NetStatus s) {
      println("netStatus (tcps) : "+s);
    }
  };

  NetListener nl2 = new NetListener() {
    public void netEvent(NetMessage m) {
      println("netEvent (tcpc) : "+ new String( m.getData() ) );
    }
    public void netStatus(NetStatus s) {
      println("netStatus (tcpc) : "+s);
    }
  };


  /* now lets create the server first. The server will listen for 
   * clients at port 4000. since we expect bytes from the client, the 3rd
   * argument (TcpServer.MODE_STREAM) is a static variable setting
   * the mode of the server to bytes (default is String);
   */
  tcps = new TcpServer( nl1, 4000, TcpServer.MODE_STREAM );
  
  /* after the server is running, create a client which will connect
   * to the newly create server at 127.0.0.1:4000 (127.0.0.1 is locahost).
   * we also need to set the mode to stream since we want to receive and
   * send bytes.
   */
  tcpc = new TcpClient( nl2, "192.168.1.16", 4000, TcpServer.MODE_STREAM );
  
}

void draw() {
}

void mousePressed(){
  KetaiKeyboard.toggle(this);
}

void keyPressed() {
  /* use key 1 and 2 to send data from the server to the client and vice versa.  */
  switch(key) {
    case( '1' ) : 
    println("sending from tcpc to tcps"); 
    tcpc.send( "Hello World from TCP Client to TCP Server.".getBytes() ); 
    break;
    case( '2' ) : 
    println("sending from tcps to tcpc"); 
    tcps.send( "Hello World from TCP Server to TCP Client.".getBytes() ); 
    break;
  }
}

@GoToLoop the problem was in the windows part of the communication, android was only the sender.

I managed to get it around by making the oscmessage variable public declaring it out of the event and trigger an event by a boolean variable inside draw to execute the code that was not syncronizing correctly