ESP8266WiFi - Connect Fails.... Sometimes....

Here is the simplest test case I could put together, which for reasons I can't understand, does not work at all. Data is sent, but never received. I thought I was starting to understand these things....

[UPDATE] This example is now working - I had forgotten to put a call to RecvUDP() in loop(). But that leaves the question - why does it NOT work in my big application? The ONLY WiFi calls in the rest of the code are UDP send and receive code, identical to what's in this example. Yet, with that code running, it does not re-connect reliably, or hardly at all....

Just program two 8266s, one programmed with AP set to 0, the other with AP set to 1. They SHOULD be sending UDP packets to each other every 10 seconds, and each should be printing any packets it receives to its Serial port. The APs ARE working, all the IPs, SSIDs, passwords, channels, etc. are correct but no data seems to pass between them. Why? What am I doing wrong?

#include <Arduino.h>
#include <stdarg.h>
#include <stdio.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>


Stream *ConsolePort = &Serial;

char* GatewaySSID = "TP-LINK_A47DC2";
char* GatewayPassword = "79517515";
uint16_t UDPPort = 8000;
WiFiUDP *UDP = NULL;

#define AP			1

#if		AP
#define STASSID		"QDNetwork0"
#define STAPASSWORD	"QDPassword0"
#define STAIP		IPAddress(192,168,0,2)
#define GATEWAYIP	IPAddress(192,168,0,1)

#define APSSID		"QDNetwork1"
#define APPASSWORD	"QDPassword1"
#define APIP		IPAddress(192,168,1,1)
#define APCHANNEL	6

#define TARGET		IPAddress(192,168,0,1)
#else
#define STASSID		"TP-LINK_A47DC2"
#define STAPASSWORD	"79517515"
#define STAIP		IPAddress(10,0,0,50)
#define GATEWAYIP	IPAddress(10,0,0,1)

#define APSSID		"QDNetwork0"
#define APPASSWORD	"QDPassword0"
#define APIP		IPAddress(192,168,0,1)
#define APCHANNEL	1

#define TARGET		IPAddress(192,168,0,2)
#endif

#define NETMASK		IPAddress(255,255,255,0)

char *statusStrings[] =
{
	"WL_IDLE_STATUS",
	"WL_NO_SSID_AVAIL",
	"WL_SCAN_COMPLETED",
	"WL_CONNECTED",
	"WL_CONNECT_FAILED",
	"WL_CONNECTION_LOST",
	"WL_DISCONNECTED",
};

void setup()
{
	delay(1000);
	((HardwareSerial *)ConsolePort)->begin(115200);
	ConsolePort->println("\n\nNODEMCU Ready\n\n");

	delay(1000);
	WiFi.begin();

	InitializeAP(APSSID, APPASSWORD, APIP, GATEWAYIP, APCHANNEL, STASSID, STAPASSWORD, STAIP);
}


boolean InitializeAP(char *apssid, char *appassword, IPAddress apip, IPAddress gatewayip, uint8_t apchannel,
					 char *stassid, char *stapassword, IPAddress staip)
{
	boolean success = false;

	while (!success)
	{
		// Set Mode
		if (!(success = WiFi.mode(WiFiMode_t::WIFI_AP_STA)))
			continue;

		// Configure STA
		if (!(success = WiFi.config(staip, gatewayip, NETMASK, gatewayip)))
			continue;

		// Configure AP
		if (!(success = WiFi.softAPConfig(apip, gatewayip, NETMASK)))
			continue;

		if (!(success = WiFi.setAutoConnect(true)))
			continue;

		if (!(success = WiFi.setAutoReconnect(true)))
			continue;

		// Start/Connect AP + STA
		if (!(success = WiFi.softAP(apssid, appassword, apchannel)))
			continue;

		if (!(success = WiFi.begin(stassid, stapassword)))
			continue;
	}

	// Setup port listener
	UDP = new WiFiUDP();
	UDP->begin(UDPPort);

	IPAddress STAip = WiFi.localIP();
	IPAddress APip = WiFi.softAPIP();
	ConsolePort->printf("AP Ready, STAIP=%d.%d.%d.%d, APIP=%d.%d.%d.%d\n", STAip[0], STAip[1], STAip[2], STAip[3], APip[0], APip[1], APip[2], APip[3]);
	delay(500);
}


boolean sendUDP(char *s)
{
	boolean ret = false;

	IPAddress ip = TARGET;
	if (ret = UDP->beginPacket(ip, UDPPort))
	{
		UDP->write(s);
		ret = UDP->endPacket();
	}
	ConsolePort->printf("status()=%s, Sent %s to %d.%d.%d.%d\n", statusStrings[WiFi.status()], s, ip[0], ip[1], ip[2], ip[3]);
	return ret;
}


void RecvUDP(void)
{
	char packetBuffer[128];
	int len;

	while (len = UDP->parsePacket())
	{
		// read the packet into packetBufffer
		len = UDP->read(packetBuffer, 128);
		packetBuffer[len] = '\0';
		ConsolePort->printf("status=%s, Received: %s\n", statusStrings[WiFi.status()], packetBuffer);
	}
}


uint32_t start1 = 0;
uint32_t start2 = 0UL - 5000UL;
void loop()
{
	if ((millis() - start1 > 10000) && (!AP))
	{
		sendUDP("Hello from AP0");
		start1 = millis();
	}
	if ((millis() - start2 > 8000) && (AP))
	{
		sendUDP("Hello from AP1");
		start2 = millis();
	}
	WiFi.status() == WL_CONNECTED;

        RecvUDP();
}

Regards,
Ray L.