user18846: I'm trying to control Philips Hue lights through an Arduino Uno (with a WiFi shield Model R3). Both the shield and the Philips Hue Bridge are connected to the same network and I'm trying to send JSON commands from the Arduino to the Hue Bridge from the following code taken from here.
#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>

char ssid[] = "mynetwork";     //  your network SSID (name)
char pass[] = "mypass";  // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

//Hue constants
const char hueHubIP[] = "192.168.0.50";  //Hue Bridge IP
const char hueUsername[] = "myhue";  //Hue username
const int hueHubPort = 80;

IPAddress server(192,168,0,50);
WiFiClient client;

//Hue variables
boolean hueOn;  // on/off
int hueBri;  // brightness value
long hueHue;  // hue value
String hueCmd;  // Hue command

unsigned long buffer=0;  //buffer for received data storage
unsigned long addr;
IPAddress ip;

void setup()
{
 //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the data:
  Serial.print("You're connected to the network");
  printCurrentNet();
  printWifiData();

  ip = WiFi.localIP();
  Serial.println(ip);

}

void loop() 
{

    String command = "{\"on\": true,\"hue\": 50100,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}";
    setHue(1,command);
    command = "{\"on\": true,\"hue\": 65280,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}"; 
    setHue(2,command);
    command = "{\"hue\": 65280,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}";
    setHue(1,command);
    command = "{\"hue\": 50100,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}"; 
    setHue(2,command);

}


/* setHue() is our main command function, which needs to be passed a light number and a 
 * properly formatted command string in JSON format (basically a Javascript style array of variables
 * and values. It then makes a simple HTTP PUT request to the Bridge at the IP specified at the start.
 */
boolean setHue(int lightNum,String command)
{
  if (client.connect(server, 80))
  {
    Serial.println(F("connected to server"));
    while (client.connected()&& status == WL_CONNECTED)
    {
      client.print(F("PUT /api/"));
      client.print(hueUsername);
      client.print(F("/lights/"));
      client.print(lightNum);  // hueLight zero based, add 1
      client.println(F("/state HTTP/1.1"));
      client.println(F("keep-alive"));
      client.print(F("Host: "));
      client.println(hueHubIP);
      client.print(F("Content-Length: "));
      client.println(command.length());
      client.println(F("Content-Type: text/plain;charset=UTF-8"));
      client.println();  // blank line before body
      client.println(command);  // Hue command
    }
    client.stop();
    return true;  // command executed
  }
  else
    return false;  // command failed
}

  //client.flush();
  //client.stop();

/* A helper function in case your logic depends on the current state of the light. 
 * This sets a number of global variables which you can check to find out if a light is currently on or not
 * and the hue etc. Not needed just to send out commands
 */
boolean getHue(int lightNum)
{
  if (client.connect(server, 80))
  {
    client.print(F("GET /api/"));
    client.print(hueUsername);
    client.print(F("/lights/"));
    client.print(lightNum);  
    client.println(F(" HTTP/1.1"));
    client.print(F("Host: "));
    client.println(hueHubIP);
    client.println(F("Content-type: application/json"));
    client.println(F("keep-alive"));
    client.println();
    while (client.connected()&& status == WL_CONNECTED)
    {
      if (client.available())
      {
        client.findUntil("\"on\":", "\0");
        hueOn = (client.readStringUntil(',') == "true");  // if light is on, set variable to true

        client.findUntil("\"bri\":", "\0");
        hueBri = client.readStringUntil(',').toInt();  // set variable to brightness value

        client.findUntil("\"hue\":", "\0");
        hueHue = client.readStringUntil(',').toInt();  // set variable to hue value

        break;  // not capturing other light attributes yet
      }
    }
    client.stop();
    return true;  // captured on,bri,hue
  }
  else
    return false;  // error reading on,bri,hue
}


void printWifiData() {

  //WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

  void printCurrentNet() {
  //SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  }

}
The above code gets my Arduino connected to mynetwork and I have also connected Hue Bridge to the same but it throws an error of No Socket Available.

UPDATE (9th March 2016):
I implemented suggestions 1 and 4 given by slash-dev and was able to get rid of No Socket Available error. Following is the updated code:
#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>

char ssid[] = "mynetwork";     //  your network SSID (name)
char pass[] = "mypass";  // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

//Hue constants
const char hueHubIP[] = "192.168.0.50";  //Hue hub IP
const char hueUsername[] = "myhue";  //Hue username
const int hueHubPort = 80;

IPAddress server(192,168,0,50);
WiFiClient client;

//Hue variables
boolean hueOn;  // on/off
int hueBri;  // brightness value
long hueHue;  // hue value
String hueCmd;  // Hue command

unsigned long buffer=0;  //buffer for received data storage
unsigned long addr;
IPAddress ip;

void setup()
{
 //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(F("WiFi shield not present"));
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print(F("Attempting to connect to WPA SSID: "));
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the data:
  Serial.print(F("You're connected to the network"));
  printCurrentNet();
  printWifiData();
}


void loop() 
{

    String command = "{\"on\": true,\"hue\": 50100,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}";
    setHue(1,command);
    command = "{\"on\": true,\"hue\": 65280,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}"; 
    setHue(2,command);
    command = "{\"hue\": 65280,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}";
    setHue(1,command);
    command = "{\"hue\": 50100,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}"; 
    setHue(2,command);

}


/* setHue() is our main command function, which needs to be passed a light number and a 
 * properly formatted command string in JSON format (basically a Javascript style array of variables
 * and values. It then makes a simple HTTP PUT request to the Bridge at the IP specified at the start.
 */
boolean setHue(int lightNum,String command)
{
  client.stop();
  if (client.connect(server, 80))
  {
    Serial.println(F("connected to server"));
    while (client.connected()&& status == WL_CONNECTED)
    {
      client.print(F("PUT /api/"));
      client.print(hueUsername);
      client.print(F("/lights/"));
      client.print(lightNum);  // hueLight zero based, add 1
      client.println(F("/state HTTP/1.1"));
      client.println(F("Connection: keep-alive"));
      client.print(F("Host: "));
      client.println(hueHubIP);
      client.print(F("Content-Length: "));
      client.println(command.length());
      client.println(F("Content-Type: text/plain;charset=UTF-8"));
      client.println();  // blank line before body
      client.println(command);  // Hue command
    }
    client.flush();
    client.stop();
    return true;  // command executed
  }
  else
    return false;  // command failed
}


/* A helper function in case your logic depends on the current state of the light. 
 * This sets a number of global variables which you can check to find out if a light is currently on or not
 * and the hue etc. Not needed just to send out commands
 */
boolean getHue(int lightNum)
{
  client.stop();
  if (client.connect(server, 80))
  {
    client.print(F("GET /api/"));
    client.print(hueUsername);
    client.print(F("/lights/"));
    client.print(lightNum);  
    client.println(F(" HTTP/1.1"));
    client.print(F("Host: "));
    client.println(hueHubIP);
    client.println(F("Content-type: application/json"));
    client.println(F("Connection: keep-alive"));
    client.println();
    while (client.connected()&& status == WL_CONNECTED)
    {
      if (client.available())
      {
        client.findUntil("\"on\":", "\0");
        hueOn = (client.readStringUntil(',') == "true");  // if light is on, set variable to true

        client.findUntil("\"bri\":", "\0");
        hueBri = client.readStringUntil(',').toInt();  // set variable to brightness value

        client.findUntil("\"hue\":", "\0");
        hueHue = client.readStringUntil(',').toInt();  // set variable to hue value

        break;  // not capturing other light attributes yet
      }
    }
    client.flush();
    client.stop();
    return true;  // captured on,bri,hue
  }
  else
    return false;  // error reading on,bri,hue
}

void printWifiData() {

  //WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print(F("IP Address: "));
  Serial.println(ip);
}

  void printCurrentNet() {
  //SSID of the network you're attached to:
  Serial.print(F("SSID: "));
  Serial.println(WiFi.SSID());
}


user11763: With reference to this example, perhaps you should call client.stop() right before you call client.connect() at the start of setHue() and getHue(). You should also call not only client.stop(), but also client.flush() at the end of both functions, just in case: Flush first, then stop. 
On a different note, correct your HTTP headers from client.println(F("keep-alive")); to client.println(F("Connection: keep-alive"));.

user14737: I suspect a low RAM failure.  Here are some suggestions that will dramatically lower your RAM usage:
1) You still have Serial.print using double-quoted C string constants.  Use F("C string") for those, too.  For example:
  Serial.println( F("WiFi shield not present") );

Every double-quoted C string uses RAM in addition to FLASH program space.  You have already saved a bunch, but you should get rid of the above uses.

2) Don't use String for setHue.
TL;DR - it will use extra RAM, FLASH, CPU time, and cause inexplicable or random failures.
Instead, use C strings, aka char arrays for variables, or double-quoted string constants.  When the constants are wrapped with the F() macro, they don't use any RAM.
Here is an example of how to define and call a setHue with C strings instead of String:
--> See complete code below in UPDATE 3/9/2016 section <--

This just prints the C strings to Serial, but I hope you can see how they could also be printed to the client.

3) Don't use String for getHue.
Here is an example of how to define and call a getHue that uses C strings instead of String:
--> See complete code below in UPDATE 3/9/2016 section <--

Just copy and paste the displayed JSON into the Serial Monitor "Send" field.  The debug prints will display what gets parsed.

NOTE: It's not clear if the JSON attributes always come back in the same order.  This example expects the same order in which they were sent.  Other techniques would have to be used to accommodate any order. 


4) Be sure to call client.flush() before every client.stop().


Summary:  I would guess that the combination of non-F() "C strings" wasting RAM, plus the String use of RAM (doubled or more), is leading to a low-RAM failure.  The above approach will lower RAM usage dramatically, and also save more than 1.6kB in program space.  After implementing these changes, almost all RAM will be available for the WiFi libraries.

UPDATE 3/9/2016
Here's my changes, merged into your second sketch.
#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>

char ssid[] = "mynetwork";     //  your network SSID (name)
char pass[] = "mypass";  // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

//Hue constants
const char hueHubIP[] = "192.168.0.50";  //Hue hub IP
const char hueUsername[] = "myhue";  //Hue username
const int hueHubPort = 80;

IPAddress server(192,168,0,50);

// Uncomment this to *really* use the Wi-Fi sheild.
// Comment this to read & write everything to the Serial Monitor for testing.
//#define REAL_WIFI
#ifdef REAL_WIFI
WiFiClient client;
#else
HardwareSerial & client = Serial; // redirect 'client' to the Serial Monitor
#endif

//Hue variables
boolean hueOn;  // on/off
int hueBri;  // brightness value
long hueHue;  // hue value
String hueCmd;  // Hue command

unsigned long buffer=0;  //buffer for received data storage
unsigned long addr;
IPAddress ip;

void setup()
{
 //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(F("WiFi shield not present"));
    // don't continue:
#ifdef REAL_WIFI
    while (true);
#else
    status = WL_CONNECTED;
#endif
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print(F("Attempting to connect to WPA SSID: "));
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the data:
  Serial.print(F("You're connected to the network"));
  printCurrentNet();
  printWifiData();
}


void loop() 
{
  setHue
    ( 1,
      F("{\"on\": true,\"hue\": 50100,\"sat\":255,\"bri\":255,\"transitiontime\":"),
      random(15,25),
      F("}")
    );

  delay( 10000 );

  setHue
    ( 2,
      F("{\"on\": true,\"hue\": 65280,\"sat\":255,\"bri\":255,\"transitiontime\":"),
      random(15,25),
      F("}")
    ); 

  delay( 10000 );

  setHue
    ( 1,
      F("{\"hue\": 65280,\"sat\":255,\"bri\":255,\"transitiontime\":"),
      random(15,25),
      F("}")
    );

  delay( 10000 );

  setHue
    ( 2,
      F("{\"hue\": 50100,\"sat\":255,\"bri\":255,\"transitiontime\":"),
      random(15,25),
      F("}")
    );

  delay( 10000 );

//  getHue( 1 );
//  getHue( 2 );
}


/* setHue() is our main command function, which needs to be passed a light number and a 
 * properly formatted command string in JSON format (basically a Javascript style array of variables
 * and values. It then makes a simple HTTP PUT request to the Bridge at the IP specified at the start.
 */
boolean setHue
  ( int                        lightNum,
    const __FlashStringHelper *header,
    int                        parameter,
    const __FlashStringHelper *trailer )
{
#ifdef REAL_WIFI
  client.stop();

  if (client.connect(server, 80))
#endif
  {

    Serial.println(F("connected to server"));
#ifdef REAL_WIFI
    if (client.connected()&& status == WL_CONNECTED)
#endif
    {
      client.print(F("PUT /api/"));
      client.print(hueUsername);
      client.print(F("/lights/"));
      client.print(lightNum);  // hueLight zero based, add 1
      client.println(F("/state HTTP/1.1"));
      client.println(F("Connection: keep-alive"));
      client.print(F("Host: "));
      client.println(hueHubIP);

      // Calculate total command length.
      int cmdLength =
        strlen_P( (const prog_char *)header ) +
        1 +   //   Assume one digit for parameter...
        strlen_P( (const prog_char *)trailer );

      //  ... and add room for more digits if necessary.
      if (parameter > 9)
        cmdLength++;
      if (parameter > 99)
        cmdLength++;

      client.print(F("Content-Length: "));
      client.println( cmdLength );
      client.println(F("Content-Type: text/plain;charset=UTF-8"));
      client.println();  // blank line before body
      client.print( header );  // Hue command pieces
      client.print( parameter );
      client.println( trailer );
    }
    client.flush();
#ifdef REAL_WIFI
    client.stop();
#endif
    return true;  // command executed
  }
#ifdef REAL_WIFI
  else
    return false;  // command failed
#endif
}

//---------------------------------------------------------------------
// Utility function to extract one `bool` value from the client stream.
//   No buffers are used.  Each character is handled as it is received.
//   (you may want/need to add a timeout)

static bool clientParseBool( char delim )
{
  bool val = false;
  uint8_t count = 0; // number of chars we've read for the value

  while (status == WL_CONNECTED) {
    if (client.available()) {
      char c = client.read();
      if (c == delim)
        break; // done!
      if ((c == ' ') || (count++ > 0))
        continue; // skip spaces and anything after the first non-space char

      if (c == 't')
        val = true;
    }
  }

  // Debug prints
  Serial.print( F("bool ") );
  Serial.println( val );

  return val;
}

//-------------------------------------------
// Utility function to extract one 'unsigned 16-bit int' value from the client stream.
//   No buffers are used.  Each character is handled as it is received.
//   (you may want/need to add a timeout)

static uint16_t clientParseUint( char delim )
{
  uint16_t val = 0;

  while (status == WL_CONNECTED) {
    if (client.available()) {
      char c = client.read();
      if (c == delim)
        break; // done!
      if (c == ' ')
        continue; // skip spaces

      if (isdigit(c))
        val = val*10 + (c - '0'); // accumulate one digit of the bri value
    }
  }

  // Debug prints
  Serial.print( F("int ") );
  Serial.println( val );

  return val;
}

//-------------------------------------------

/* A helper function in case your logic depends on the current state of the light. 
 * This sets a number of global variables which you can check to find out if a light is currently on or not
 * and the hue etc. Not needed just to send out commands
 */
boolean getHue(int lightNum)
{
  bool gotHue = false;

#ifdef REAL_WIFI
  client.stop();
  if (client.connect(server, 80))
#endif
  {

    client.print(F("GET /api/"));
    client.print(hueUsername);
    client.print(F("/lights/"));
    client.print(lightNum);  
    client.println(F(" HTTP/1.1"));
    client.print(F("Host: "));
    client.println(hueHubIP);
    client.println(F("Content-type: application/json"));
    client.println(F("Connection: keep-alive"));
    client.println();

    if (status == WL_CONNECTED)
    {
        client.findUntil("\"on\":", "\0");
        hueOn = clientParseBool(',');

        if (status == WL_CONNECTED)
        {
            client.findUntil("\"bri\":", "\0");
            hueBri = clientParseUint(',');

            if (status == WL_CONNECTED)
            {
                client.findUntil("\"hue\":", "\0");
                hueHue = clientParseUint(',');

                gotHue = true;  // captured all three: on,bri,hue
            }
        }
    }

    client.flush();
#ifdef REAL_WIFI
    client.stop();
#endif
  }

  return gotHue;
}

void printWifiData() {

  //WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print(F("IP Address: "));
  Serial.println(ip);
}

void printCurrentNet() {
  //SSID of the network you're attached to:
  Serial.print(F("SSID: "));
  Serial.println(WiFi.SSID());
}

A few notes:

I did not have the extra blank line in my original setHue, so I put it back in to match your sketch.
I added a REAL_WIFI define that I could comment out for testing on Serial.  When I run it, I get this output:


WiFi shield not present
  You're connected to the networkSSID:
  IP Address: 0.0.0.0
  connected to server
  PUT /api/myhue/lights/1/state HTTP/1.1
  Connection: keep-alive
  Host: 192.168.0.50
  Content-Length: 65
  Content-Type: text/plain;charset=UTF-8

  {"on": true,"hue": 50100,"sat":255,"bri":255,"transitiontime":22}
  connected to server
  PUT /api/myhue/lights/2/state HTTP/1.1
  Connection: keep-alive
  Host: 192.168.0.50
  Content-Length: 65
  Content-Type: text/plain;charset=UTF-8

  {"on": true,"hue": 65280,"sat":255,"bri":255,"transitiontime":24}

You can copy and paste the above output if you want to add and test getHue with just the Serial Monitor (i.e., comment out #define REAL_WIFI).
I added a 10-second delay between each setHue, just to pace the output, and to give the "transition-time" um... some time to work.  The transition time is in seconds, so loop will call setHue again, before the requested transition has completed.  You won't see much change when you keep telling it to gradually change to the new color, but it only gets to transition a little bit.  Without any delay, I don't think you'll see anything.  This could be your current problem!  With 10 seconds, at least it will get about half-way there.  :-(


user10794: I wonder if you are running out of RAM? Those sockets use RAM, and you have quite a few string constants, like this:
  client.println("Content-Type: text/plain;charset=UTF-8");

Change that (and all similar ones) to use the F() macro:
  client.println(F("Content-Type: text/plain;charset=UTF-8"));


18846: <p>I'm trying to control Philips Hue lights through an Arduino Uno (with a WiFi shield <a href="https://www.arduino.cc/en/Main/ArduinoWiFiShield" rel="nofollow noreferrer">Model R3</a>). Both the shield and the Philips Hue <a href="http://www2.meethue.com/en-us/productdetail/philips-hue-bridge" rel="nofollow noreferrer">Bridge</a> are connected to the same network and I'm trying to send JSON commands from the Arduino to the Hue Bridge from the following code taken from <a href="http://www.makeuseof.com/tag/control-philips-hue-lights-arduino-and-motion-sensor#WorkingFromArduino" rel="nofollow noreferrer">here</a>.</p>

<pre class="lang-c prettyprint-override"><code>#include &lt;SPI.h&gt;
#include &lt;WiFi.h&gt;
#include &lt;WiFiClient.h&gt;

char ssid[] = "mynetwork";     //  your network SSID (name)
char pass[] = "mypass";  // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

//Hue constants
const char hueHubIP[] = "192.168.0.50";  //Hue Bridge IP
const char hueUsername[] = "myhue";  //Hue username
const int hueHubPort = 80;

IPAddress server(192,168,0,50);
WiFiClient client;

//Hue variables
boolean hueOn;  // on/off
int hueBri;  // brightness value
long hueHue;  // hue value
String hueCmd;  // Hue command

unsigned long buffer=0;  //buffer for received data storage
unsigned long addr;
IPAddress ip;

void setup()
{
 //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the data:
  Serial.print("You're connected to the network");
  printCurrentNet();
  printWifiData();

  ip = WiFi.localIP();
  Serial.println(ip);

}

void loop() 
{

    String command = "{\"on\": true,\"hue\": 50100,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}";
    setHue(1,command);
    command = "{\"on\": true,\"hue\": 65280,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}"; 
    setHue(2,command);
    command = "{\"hue\": 65280,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}";
    setHue(1,command);
    command = "{\"hue\": 50100,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}"; 
    setHue(2,command);

}


/* setHue() is our main command function, which needs to be passed a light number and a 
 * properly formatted command string in JSON format (basically a Javascript style array of variables
 * and values. It then makes a simple HTTP PUT request to the Bridge at the IP specified at the start.
 */
boolean setHue(int lightNum,String command)
{
  if (client.connect(server, 80))
  {
    Serial.println(F("connected to server"));
    while (client.connected()&amp;&amp; status == WL_CONNECTED)
    {
      client.print(F("PUT /api/"));
      client.print(hueUsername);
      client.print(F("/lights/"));
      client.print(lightNum);  // hueLight zero based, add 1
      client.println(F("/state HTTP/1.1"));
      client.println(F("keep-alive"));
      client.print(F("Host: "));
      client.println(hueHubIP);
      client.print(F("Content-Length: "));
      client.println(command.length());
      client.println(F("Content-Type: text/plain;charset=UTF-8"));
      client.println();  // blank line before body
      client.println(command);  // Hue command
    }
    client.stop();
    return true;  // command executed
  }
  else
    return false;  // command failed
}

  //client.flush();
  //client.stop();

/* A helper function in case your logic depends on the current state of the light. 
 * This sets a number of global variables which you can check to find out if a light is currently on or not
 * and the hue etc. Not needed just to send out commands
 */
boolean getHue(int lightNum)
{
  if (client.connect(server, 80))
  {
    client.print(F("GET /api/"));
    client.print(hueUsername);
    client.print(F("/lights/"));
    client.print(lightNum);  
    client.println(F(" HTTP/1.1"));
    client.print(F("Host: "));
    client.println(hueHubIP);
    client.println(F("Content-type: application/json"));
    client.println(F("keep-alive"));
    client.println();
    while (client.connected()&amp;&amp; status == WL_CONNECTED)
    {
      if (client.available())
      {
        client.findUntil("\"on\":", "\0");
        hueOn = (client.readStringUntil(',') == "true");  // if light is on, set variable to true

        client.findUntil("\"bri\":", "\0");
        hueBri = client.readStringUntil(',').toInt();  // set variable to brightness value

        client.findUntil("\"hue\":", "\0");
        hueHue = client.readStringUntil(',').toInt();  // set variable to hue value

        break;  // not capturing other light attributes yet
      }
    }
    client.stop();
    return true;  // captured on,bri,hue
  }
  else
    return false;  // error reading on,bri,hue
}


void printWifiData() {

  //WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

  void printCurrentNet() {
  //SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  }
</code></pre>

<p>}</p>

<p>The above code gets my Arduino connected to <code>mynetwork</code> and I have also connected Hue Bridge to the same but it throws an error of <code>No Socket Available</code>.</p>

<p><a href="https://i.stack.imgur.com/Tub4E.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tub4E.png" alt="Error message"></a></p>

<p><strong>UPDATE (9th March 2016):</strong></p>

<p>I implemented suggestions <code>1</code> and <code>4</code> given by slash-dev and was able to get rid of <code>No Socket Available</code> error. Following is the updated code:</p>

   <pre class="lang-c prettyprint-override"><code>#include &lt;SPI.h&gt;
#include &lt;WiFi.h&gt;
#include &lt;WiFiClient.h&gt;

char ssid[] = "mynetwork";     //  your network SSID (name)
char pass[] = "mypass";  // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

//Hue constants
const char hueHubIP[] = "192.168.0.50";  //Hue hub IP
const char hueUsername[] = "myhue";  //Hue username
const int hueHubPort = 80;

IPAddress server(192,168,0,50);
WiFiClient client;

//Hue variables
boolean hueOn;  // on/off
int hueBri;  // brightness value
long hueHue;  // hue value
String hueCmd;  // Hue command

unsigned long buffer=0;  //buffer for received data storage
unsigned long addr;
IPAddress ip;

void setup()
{
 //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(F("WiFi shield not present"));
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print(F("Attempting to connect to WPA SSID: "));
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the data:
  Serial.print(F("You're connected to the network"));
  printCurrentNet();
  printWifiData();
}


void loop() 
{

    String command = "{\"on\": true,\"hue\": 50100,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}";
    setHue(1,command);
    command = "{\"on\": true,\"hue\": 65280,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}"; 
    setHue(2,command);
    command = "{\"hue\": 65280,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}";
    setHue(1,command);
    command = "{\"hue\": 50100,\"sat\":255,\"bri\":255,\"transitiontime\":"+String(random(15,25))+"}"; 
    setHue(2,command);

}


/* setHue() is our main command function, which needs to be passed a light number and a 
 * properly formatted command string in JSON format (basically a Javascript style array of variables
 * and values. It then makes a simple HTTP PUT request to the Bridge at the IP specified at the start.
 */
boolean setHue(int lightNum,String command)
{
  client.stop();
  if (client.connect(server, 80))
  {
    Serial.println(F("connected to server"));
    while (client.connected()&amp;&amp; status == WL_CONNECTED)
    {
      client.print(F("PUT /api/"));
      client.print(hueUsername);
      client.print(F("/lights/"));
      client.print(lightNum);  // hueLight zero based, add 1
      client.println(F("/state HTTP/1.1"));
      client.println(F("Connection: keep-alive"));
      client.print(F("Host: "));
      client.println(hueHubIP);
      client.print(F("Content-Length: "));
      client.println(command.length());
      client.println(F("Content-Type: text/plain;charset=UTF-8"));
      client.println();  // blank line before body
      client.println(command);  // Hue command
    }
    client.flush();
    client.stop();
    return true;  // command executed
  }
  else
    return false;  // command failed
}


/* A helper function in case your logic depends on the current state of the light. 
 * This sets a number of global variables which you can check to find out if a light is currently on or not
 * and the hue etc. Not needed just to send out commands
 */
boolean getHue(int lightNum)
{
  client.stop();
  if (client.connect(server, 80))
  {
    client.print(F("GET /api/"));
    client.print(hueUsername);
    client.print(F("/lights/"));
    client.print(lightNum);  
    client.println(F(" HTTP/1.1"));
    client.print(F("Host: "));
    client.println(hueHubIP);
    client.println(F("Content-type: application/json"));
    client.println(F("Connection: keep-alive"));
    client.println();
    while (client.connected()&amp;&amp; status == WL_CONNECTED)
    {
      if (client.available())
      {
        client.findUntil("\"on\":", "\0");
        hueOn = (client.readStringUntil(',') == "true");  // if light is on, set variable to true

        client.findUntil("\"bri\":", "\0");
        hueBri = client.readStringUntil(',').toInt();  // set variable to brightness value

        client.findUntil("\"hue\":", "\0");
        hueHue = client.readStringUntil(',').toInt();  // set variable to hue value

        break;  // not capturing other light attributes yet
      }
    }
    client.flush();
    client.stop();
    return true;  // captured on,bri,hue
  }
  else
    return false;  // error reading on,bri,hue
}

void printWifiData() {

  //WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print(F("IP Address: "));
  Serial.println(ip);
}

  void printCurrentNet() {
  //SSID of the network you're attached to:
  Serial.print(F("SSID: "));
  Serial.println(WiFi.SSID());
}
</code></pre>
 11763: <p>With reference to <a href="https://www.arduino.cc/en/Tutorial/WiFiWebClientRepeating" rel="nofollow">this example</a>, perhaps you should call <code>client.stop()</code> right before you call <code>client.connect()</code> at the start of <code>setHue()</code> and <code>getHue()</code>. You should also call not only <code>client.stop()</code>, but also <code>client.flush()</code> at the end of both functions, just in case: Flush first, then stop. </p>

<p>On a different note, correct your HTTP headers from <code>client.println(F("keep-alive"));</code> to <code>client.println(F("Connection: keep-alive"));</code>.</p>
 14737: <p>I suspect a low RAM failure.  Here are some suggestions that will dramatically lower your RAM usage:</p>

<p><strong>1)</strong> You still have <code>Serial.print</code> using double-quoted C string constants.  Use <code>F("C string")</code> for those, too.  For example:</p>

<pre><code>  Serial.println( F("WiFi shield not present") );
</code></pre>

<p>Every double-quoted C string uses RAM in addition to FLASH program space.  You have already saved a bunch, but you should get rid of the above uses.
<hr>
<strong>2) Don't use <code>String</code> for <code>setHue</code>.</strong></p>

<p>TL;DR - it will use extra RAM, FLASH, CPU time, and cause inexplicable or random failures.</p>

<p>Instead, use C strings, aka <code>char</code> arrays for variables, or double-quoted string constants.  When the constants are wrapped with the <strong>F()</strong> macro, they don't use <em>any</em> RAM.</p>

<p>Here is an example of how to define and call a <code>setHue</code> with C strings instead of <code>String</code>:</p>

<pre><code>--&gt; See complete code below in UPDATE 3/9/2016 section &lt;--
</code></pre>

<p>This just prints the C strings to <code>Serial</code>, but I hope you can see how they could also be printed to the <code>client</code>.
<hr>
<strong>3) Don't use <code>String</code> for <code>getHue</code>.</strong></p>

<p>Here is an example of how to define and call a <code>getHue</code> that uses C strings instead of <code>String</code>:</p>

<pre><code>--&gt; See complete code below in UPDATE 3/9/2016 section &lt;--
</code></pre>

<p>Just copy and paste the displayed JSON into the Serial Monitor "Send" field.  The debug prints will display what gets parsed.</p>

<ul>
<li>NOTE: It's not clear if the JSON attributes always come back in the same order.  This example expects the same order in which they were sent.  Other techniques would have to be used to accommodate <em>any</em> order. </li>
</ul>

<p><hr>
<strong>4)</strong> Be sure to call <code>client.flush()</code> before <em>every</em> <code>client.stop()</code>.</p>

<p><hr>
<strong>Summary:</strong>  I would guess that the combination of non-F() "C strings" wasting RAM, plus the <code>String</code> use of RAM (doubled or more), is leading to a low-RAM failure.  The above approach will lower RAM usage dramatically, and also save more than 1.6kB in program space.  After implementing these changes, almost all RAM will be available for the WiFi libraries.
<hr></p>

<h2>UPDATE 3/9/2016</h2>

<p>Here's my changes, merged into your second sketch.</p>

<pre><code>#include &lt;SPI.h&gt;
#include &lt;WiFi.h&gt;
#include &lt;WiFiClient.h&gt;

char ssid[] = "mynetwork";     //  your network SSID (name)
char pass[] = "mypass";  // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

//Hue constants
const char hueHubIP[] = "192.168.0.50";  //Hue hub IP
const char hueUsername[] = "myhue";  //Hue username
const int hueHubPort = 80;

IPAddress server(192,168,0,50);

// Uncomment this to *really* use the Wi-Fi sheild.
// Comment this to read &amp; write everything to the Serial Monitor for testing.
//#define REAL_WIFI
#ifdef REAL_WIFI
WiFiClient client;
#else
HardwareSerial &amp; client = Serial; // redirect 'client' to the Serial Monitor
#endif

//Hue variables
boolean hueOn;  // on/off
int hueBri;  // brightness value
long hueHue;  // hue value
String hueCmd;  // Hue command

unsigned long buffer=0;  //buffer for received data storage
unsigned long addr;
IPAddress ip;

void setup()
{
 //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(F("WiFi shield not present"));
    // don't continue:
#ifdef REAL_WIFI
    while (true);
#else
    status = WL_CONNECTED;
#endif
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print(F("Attempting to connect to WPA SSID: "));
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the data:
  Serial.print(F("You're connected to the network"));
  printCurrentNet();
  printWifiData();
}


void loop() 
{
  setHue
    ( 1,
      F("{\"on\": true,\"hue\": 50100,\"sat\":255,\"bri\":255,\"transitiontime\":"),
      random(15,25),
      F("}")
    );

  delay( 10000 );

  setHue
    ( 2,
      F("{\"on\": true,\"hue\": 65280,\"sat\":255,\"bri\":255,\"transitiontime\":"),
      random(15,25),
      F("}")
    ); 

  delay( 10000 );

  setHue
    ( 1,
      F("{\"hue\": 65280,\"sat\":255,\"bri\":255,\"transitiontime\":"),
      random(15,25),
      F("}")
    );

  delay( 10000 );

  setHue
    ( 2,
      F("{\"hue\": 50100,\"sat\":255,\"bri\":255,\"transitiontime\":"),
      random(15,25),
      F("}")
    );

  delay( 10000 );

//  getHue( 1 );
//  getHue( 2 );
}


/* setHue() is our main command function, which needs to be passed a light number and a 
 * properly formatted command string in JSON format (basically a Javascript style array of variables
 * and values. It then makes a simple HTTP PUT request to the Bridge at the IP specified at the start.
 */
boolean setHue
  ( int                        lightNum,
    const __FlashStringHelper *header,
    int                        parameter,
    const __FlashStringHelper *trailer )
{
#ifdef REAL_WIFI
  client.stop();

  if (client.connect(server, 80))
#endif
  {

    Serial.println(F("connected to server"));
#ifdef REAL_WIFI
    if (client.connected()&amp;&amp; status == WL_CONNECTED)
#endif
    {
      client.print(F("PUT /api/"));
      client.print(hueUsername);
      client.print(F("/lights/"));
      client.print(lightNum);  // hueLight zero based, add 1
      client.println(F("/state HTTP/1.1"));
      client.println(F("Connection: keep-alive"));
      client.print(F("Host: "));
      client.println(hueHubIP);

      // Calculate total command length.
      int cmdLength =
        strlen_P( (const prog_char *)header ) +
        1 +   //   Assume one digit for parameter...
        strlen_P( (const prog_char *)trailer );

      //  ... and add room for more digits if necessary.
      if (parameter &gt; 9)
        cmdLength++;
      if (parameter &gt; 99)
        cmdLength++;

      client.print(F("Content-Length: "));
      client.println( cmdLength );
      client.println(F("Content-Type: text/plain;charset=UTF-8"));
      client.println();  // blank line before body
      client.print( header );  // Hue command pieces
      client.print( parameter );
      client.println( trailer );
    }
    client.flush();
#ifdef REAL_WIFI
    client.stop();
#endif
    return true;  // command executed
  }
#ifdef REAL_WIFI
  else
    return false;  // command failed
#endif
}

//---------------------------------------------------------------------
// Utility function to extract one `bool` value from the client stream.
//   No buffers are used.  Each character is handled as it is received.
//   (you may want/need to add a timeout)

static bool clientParseBool( char delim )
{
  bool val = false;
  uint8_t count = 0; // number of chars we've read for the value

  while (status == WL_CONNECTED) {
    if (client.available()) {
      char c = client.read();
      if (c == delim)
        break; // done!
      if ((c == ' ') || (count++ &gt; 0))
        continue; // skip spaces and anything after the first non-space char

      if (c == 't')
        val = true;
    }
  }

  // Debug prints
  Serial.print( F("bool ") );
  Serial.println( val );

  return val;
}

//-------------------------------------------
// Utility function to extract one 'unsigned 16-bit int' value from the client stream.
//   No buffers are used.  Each character is handled as it is received.
//   (you may want/need to add a timeout)

static uint16_t clientParseUint( char delim )
{
  uint16_t val = 0;

  while (status == WL_CONNECTED) {
    if (client.available()) {
      char c = client.read();
      if (c == delim)
        break; // done!
      if (c == ' ')
        continue; // skip spaces

      if (isdigit(c))
        val = val*10 + (c - '0'); // accumulate one digit of the bri value
    }
  }

  // Debug prints
  Serial.print( F("int ") );
  Serial.println( val );

  return val;
}

//-------------------------------------------

/* A helper function in case your logic depends on the current state of the light. 
 * This sets a number of global variables which you can check to find out if a light is currently on or not
 * and the hue etc. Not needed just to send out commands
 */
boolean getHue(int lightNum)
{
  bool gotHue = false;

#ifdef REAL_WIFI
  client.stop();
  if (client.connect(server, 80))
#endif
  {

    client.print(F("GET /api/"));
    client.print(hueUsername);
    client.print(F("/lights/"));
    client.print(lightNum);  
    client.println(F(" HTTP/1.1"));
    client.print(F("Host: "));
    client.println(hueHubIP);
    client.println(F("Content-type: application/json"));
    client.println(F("Connection: keep-alive"));
    client.println();

    if (status == WL_CONNECTED)
    {
        client.findUntil("\"on\":", "\0");
        hueOn = clientParseBool(',');

        if (status == WL_CONNECTED)
        {
            client.findUntil("\"bri\":", "\0");
            hueBri = clientParseUint(',');

            if (status == WL_CONNECTED)
            {
                client.findUntil("\"hue\":", "\0");
                hueHue = clientParseUint(',');

                gotHue = true;  // captured all three: on,bri,hue
            }
        }
    }

    client.flush();
#ifdef REAL_WIFI
    client.stop();
#endif
  }

  return gotHue;
}

void printWifiData() {

  //WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print(F("IP Address: "));
  Serial.println(ip);
}

void printCurrentNet() {
  //SSID of the network you're attached to:
  Serial.print(F("SSID: "));
  Serial.println(WiFi.SSID());
}
</code></pre>

<p>A few notes:</p>

<ul>
<li>I did not have the extra blank line in my original <code>setHue</code>, so I put it back in to match your sketch.</li>
<li>I added a <code>REAL_WIFI</code> define that I could comment out for testing on <code>Serial</code>.  When I run it, I get this output:

<blockquote>
  <p>WiFi shield not present<br>
  You're connected to the networkSSID:<br>
  IP Address: 0.0.0.0<br>
  connected to server<br>
  PUT /api/myhue/lights/1/state HTTP/1.1<br>
  Connection: keep-alive<br>
  Host: 192.168.0.50<br>
  Content-Length: 65<br>
  Content-Type: text/plain;charset=UTF-8<br>
  <br>
  {"on": true,"hue": 50100,"sat":255,"bri":255,"transitiontime":22}<br>
  connected to server<br>
  PUT /api/myhue/lights/2/state HTTP/1.1<br>
  Connection: keep-alive<br>
  Host: 192.168.0.50<br>
  Content-Length: 65<br>
  Content-Type: text/plain;charset=UTF-8<br>
  <br>
  {"on": true,"hue": 65280,"sat":255,"bri":255,"transitiontime":24}<br></p>
</blockquote></li>
<li>You can copy and paste the above output if you want to add and test <code>getHue</code> with just the Serial Monitor (i.e., comment out <code>#define REAL_WIFI</code>).</li>
<li>I added a 10-second delay between each <code>setHue</code>, just to pace the output, and to give the "transition-time" um... some time to work.  The transition time is in seconds, so <code>loop</code> will call <code>setHue</code> again, <em>before</em> the requested transition has completed.  You won't see much change when you keep telling it to gradually change to the new color, but it only gets to transition a little bit.  Without any delay, I don't think you'll see anything.  This could be your current problem!  With 10 seconds, at least it will get about half-way there.  :-(</li>
</ul>
 10794: <p>I wonder if you are running out of RAM? Those sockets use RAM, and you have quite a few string constants, like this:</p>



<pre class="lang-C++ prettyprint-override"><code>  client.println("Content-Type: text/plain;charset=UTF-8");
</code></pre>

<p>Change that (and all similar ones) to use the <code>F()</code> macro:</p>

<pre class="lang-C++ prettyprint-override"><code>  client.println(F("Content-Type: text/plain;charset=UTF-8"));
</code></pre>
