WiFi Shield - Client Shield unable to connect

My setup is an Arduino Mega and a Raspberry Pi. connected via WiFi, I want to send some strings to and fro each other, so I used socket programming. I have a python script which is listening to the same port and awaiting to accept any connection but my Arduino just cannot connect and after 4 tries or so, it prints ‘No Socket Available’. Anybody has any solutions to this? My WiFi Shield firmware is 1.1.0.

I’ve done a few troubleshoot testing but still to no avail. With the python script running on my Rpi, I connect my laptop to the same network, and entered and the script was able to detect the connection successfully, thus I do not think there is a problem on my Rpi side.

There is no error, but it just keeps returning False from client.connect which I don’t understand why. Please helpppppp

Urgently need help on this. Thanks in advance!

Arduino Code

#include <SPI.h>
#include <SD.h>
#include <WiFi.h>
#include <WiFiUdp.h>

#define SD_CS 4

// For WiFi Shield
int status = WL_IDLE_STATUS;
char ssid[] = "lightstick"; //  your network SSID (name)
char pass[] = "lightstick";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)
unsigned int localPort = 5143;      // local port to listen on
char packetBuffer[1750]; //buffer to hold incoming packet
char temp[7];
char  ReplyBuffer[] = "ac";       // a string to send back
IPAddress my_ip(192, 168, 70, 7);
IPAddress remote_ip(192, 168, 70, 1);
WiFiUDP Udp;
WiFiClient client;
String recvData = "";

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

  String fv = WiFi.firmwareVersion();  
  // attempt to connect to Wifi network:
  if (status != WL_CONNECTED) {
    Serial.print(F("Attempting to connect to SSID: "));
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    while (status != WL_CONNECTED) {
      Serial.println("Attempting to connect again...");
      status = WiFi.begin(ssid, pass);
    Serial.println(F("WiFi establish connection ended!"));
  fv = WiFi.firmwareVersion();
  if ( fv != "1.1.0" )
    Serial.println("Please upgrade the firmware");
  boolean rpiConnected = false;
  if (status == WL_CONNECTED) {
    Serial.println(F("Connected to wifi\n"));
    rpiConnected = false;

    // Connection to rPi
    while (rpiConnected == false) {
      Serial.println(F("Connecting to rPi..."));
      rpiConnected = client.connect(remote_ip, 5143);
      Serial.println(F("\tWaiting for delay"));
    Serial.println("Connected to Raspberry Pi");
    sendData("Arduino Connected");

  } else {
    Serial.println(F("Wifi connection FAILED!"));

Python Script

import thread
import time
import socket

from collections import deque


def ipComm():
    print "Waiting for Arduino"

    print "Starting socket connection."
    ip = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    print "Started! Setting socket option, reuse address"
    ip.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    # ip.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    hostname = socket.gethostname()
    print hostname
    hostip = socket.gethostbyname_ex(hostname)
    print hostip
    print "Set! Starting socket binding"
    print RPI_HOST
    print "Binded! Starting socket listening"
    print "Listening...."

    data, pcaddr = ip.accept()
    print "Connected to Arduino", pcaddr
    return data, pcaddr