TCPClient to remote server connection issue

Hello Particle Community!
I’ve picked up my Photon about a day ago and have been writing an application to monitor the weather with an RGB LED by receiving sky conditions across the internet to a remote server, however I seem to be stuck on a socket specific issue here that has been keeping me up for the past few hours.

The Issue:
When the Photon attempts to connect through a void function, the server does get the response:

[98.77.7*.] has connected
] server is gathering JSON data
[98.77.7*.] assuming ZIP Code is 349xx
] using location code: 151xx_PC
[98.77.7*.] has been sent sky condition: Mostly clear
] has been disconnected.

However, using “Particle.publish”, in the else conditional on being connected returns to the dashboard that the particle has not connected to the server. (Screenshot of logs:


bool silenceLogger = false;

//TCPClient Variables
TCPClient client;
String response;

//Main Code
String getWeatherData() {
    if (!silenceLogger){
        Particle.publish("Task", "Gathering Weather Data");    
    //clear the response
    response = "";
    client.connect("", 8744);
    if (client.connected()) {
        while (client.available()){
            response += String(;
        //Check the response
        if (response == "" || response == "error"){
            silenceLogger = true;
            return "error";
        } else {
            Particle.publish("Weather Data", response);
            return response;    
    } else {
        Particle.publish("Error", "Failed to connect to socket server.");
        return "error";

Can someone please offer advice on how to solve this issue or suggest code?

Thought I would add this: if you would like to attempt to test the issue with my code, you can use my socket server which will respond with the weather data of your location using geolocation after sending the string “condition!”, at your own consent.
(, 8744)

Update: I have solved the issue.

Two things I have performed were re-writing the code to be more simplified, and adding a delay to the while loop.
I did not take into any consideration latency, and these tweaks have fixed it.