if (hasRPort || !hop.getHost().equals(this.peerAddress) ) {
if (hasRPort || !hop.getHost().equals(this.peerAddress.getHostAddress()) ) {
	public void run() {		while (true) {			// Create a new string message parser to parse the list of messages.			if (myParser == null) {				myParser = new StringMsgParser();				myParser.setParseExceptionListener(this);			}			// messages that we write out to him.			DatagramPacket packet;			if (sipStack.threadPoolSize != -1) {				synchronized (((UDPMessageProcessor) messageProcessor).messageQueue) {					while (((UDPMessageProcessor) messageProcessor).messageQueue							.isEmpty()) {						// Check to see if we need to exit.						if (!((UDPMessageProcessor) messageProcessor).isRunning)							return;						try {							((UDPMessageProcessor) messageProcessor).messageQueue									.wait();						} catch (InterruptedException ex) {							if (!((UDPMessageProcessor) messageProcessor).isRunning)								return;						}					}					packet = (DatagramPacket) ((UDPMessageProcessor) messageProcessor).messageQueue							.removeFirst();				}				this.incomingPacket = packet;			} else {				packet = this.incomingPacket;			}			this.peerAddress = packet.getAddress();			int packetLength = packet.getLength();			// Read bytes and put it in a eueue.			byte[] bytes = packet.getData();			byte[] msgBytes = new byte[packetLength];			System.arraycopy(bytes, 0, msgBytes, 0, packetLength);			// Do debug logging.			if (sipStack.isLoggingEnabled()) {				this.sipStack.logWriter						.logDebug("UDPMessageChannel: peerAddress = "								+ peerAddress.getHostAddress() + "/"								+ packet.getPort());				this.sipStack.logWriter.logDebug("Length = " + packetLength);				String msgString = new String(msgBytes, 0, packetLength);				this.sipStack.logWriter.logDebug(msgString);			}			SIPMessage sipMessage = null;			try {				this.receptionTime = System.currentTimeMillis();				sipMessage = myParser.parseSIPMessage(msgBytes);				myParser = null;			} catch (ParseException ex) {				myParser = null; // let go of the parser reference.				if (sipStack.isLoggingEnabled()) {					this.sipStack.logWriter.logDebug("Rejecting message !  "							+ new String(msgBytes));					this.sipStack.logWriter.logDebug("error message "							+ ex.getMessage());					this.sipStack.logWriter.logException(ex);				}				if (sipStack.isLoggingEnabled()) {					sipStack.getLogWriter().logDebug(new String(msgBytes));				}				if (sipStack.threadPoolSize == -1)					return;				else					continue;			}			// No parse exception but null message - reject it and			// march on (or return). 			// exit this message processor if the message did not parse.			if (sipMessage == null) {				if (sipStack.isLoggingEnabled()) {					this.sipStack.logWriter.logDebug("Rejecting message !  "							+ new String(msgBytes));					this.sipStack.logWriter.logDebug("Null message parsed.");				}				if (sipStack.threadPoolSize == -1)					return;				else					continue;			}			ViaList viaList = sipMessage.getViaHeaders();			// Check for the required headers.			if (sipMessage.getFrom() == null					|| sipMessage.getTo() == null					|| sipMessage.getCallId() == null					|| sipMessage.getCSeq() == null					|| sipMessage.getViaHeaders() == null) {				String badmsg = new String(msgBytes);				if (sipStack.isLoggingEnabled()) {					this.sipStack.logWriter.logError("bad message " + badmsg);					this.sipStack.logWriter.logError(">>> Dropped Bad Msg "							+ "From = " + sipMessage.getFrom() + "To = "							+ sipMessage.getTo() + "CallId = "							+ sipMessage.getCallId() + "CSeq = "							+ sipMessage.getCSeq() + "Via = "							+ sipMessage.getViaHeaders());				}				sipStack.logWriter.logError("BAD MESSAGE!");				sipStack.logWriter.logError(badmsg);				if (sipStack.threadPoolSize == -1)					return;				else					continue;			}			// For a request first via header tells where the message			// is coming from.			// For response, just get the port from the packet.			if (sipMessage instanceof SIPRequest) {				Via v = (Via) viaList.getFirst();				Hop hop = sipStack.addressResolver.resolveAddress(v.getHop());				this.peerPort = hop.getPort();				this.peerProtocol = v.getTransport();				this.peerPacketSourceAddress = packet.getAddress();				this.peerPacketSourcePort = packet.getPort();				try {					this.peerAddress = packet.getAddress();					// Check to see if the received parameter matches					// the peer address and tag it appropriately.					// JvB: Better not do a DNS lookup here, this is costly					// InetAddress sentByAddress = InetAddress.getByName(hop.getHost());										boolean hasRPort = v.hasParameter(Via.RPORT);										if (hasRPort || !hop.getHost().equals(this.peerAddress) ) {						v.setParameter(Via.RECEIVED, this.peerAddress.getHostAddress());					}										if (hasRPort) {						v.setParameter(Via.RPORT, Integer.toString(								this.peerPacketSourcePort) );					}				} catch (java.text.ParseException ex1) {					InternalErrorHandler.handleException(ex1);				}			} else {								this.peerPacketSourceAddress = packet.getAddress();				this.peerPacketSourcePort = packet.getPort();				this.peerAddress = packet.getAddress();				this.peerPort = packet.getPort();				this.peerProtocol = ((Via) viaList.getFirst()).getTransport();			}			if (sipMessage instanceof SIPRequest) {				SIPRequest sipRequest = (SIPRequest) sipMessage;				// This is a request - process it.				//So far so good -- we will commit this message if				// all processing is OK.				if (sipStack.serverLog.needsLogging(ServerLog.TRACE_MESSAGES)) {										this.sipStack.serverLog.logMessage(sipMessage, 							this.getPeerHostPort().toString(), this.getHost()							+ ":" + this.myPort, false, new Long(receptionTime)							.toString());				}				ServerRequestInterface sipServerRequest = sipStack						.newSIPServerRequest(sipRequest, this);				// Drop it if there is no request returned				if (sipServerRequest == null) {					if (sipStack.isLoggingEnabled()) {						this.sipStack.logWriter								.logWarning("Null request interface returned -- dropping request");					}										if (!sipRequest.getMethod().equals(Request.ACK)) {	  					SIPResponse response = sipRequest.createResponse(Response.SERVICE_UNAVAILABLE);	  					response.addHeader(sipStack.createServerHeaderForStack());	  					RetryAfter retryAfter = new RetryAfter();	  					  					// Be a good citizen and send a decent response code back.	  					try {	  						retryAfter.setRetryAfter((int)(10 * (Math.random())));	  						response.setHeader(retryAfter);	  						this.sendMessage(response);	  					} catch (Exception e) {	  						this.sipStack.logWriter.logError( "Exception while sending service_unavailable", e );	  					}	  				}					continue;				}				if (sipStack.isLoggingEnabled())					this.sipStack.logWriter.logDebug("About to process "							+ sipRequest.getFirstLine() + "/"							+ sipServerRequest);				try {					sipServerRequest.processRequest(sipRequest, this);				} finally {					if (sipServerRequest instanceof SIPTransaction) {						SIPServerTransaction sipServerTx = (SIPServerTransaction) sipServerRequest;						if (! sipServerTx.passToListener()) {							((SIPTransaction) sipServerRequest).releaseSem();						}					}				}				if (sipStack.isLoggingEnabled())					this.sipStack.logWriter.logDebug("Done processing "							+ sipRequest.getFirstLine() + "/"							+ sipServerRequest);				// So far so good -- we will commit this message if				// all processing is OK.							} else {				// Handle a SIP Reply message.				SIPResponse sipResponse = (SIPResponse) sipMessage;				if (sipResponse.getStatusCode() == 100) sipResponse.getTo().removeParameter("tag");				try {					sipResponse.checkHeaders();				} catch (ParseException ex) {					if (sipStack.isLoggingEnabled())						sipStack.logWriter.logError("Dropping Badly formatted response message >>> " + sipResponse);					return;				}				ServerResponseInterface sipServerResponse = sipStack						.newSIPServerResponse(sipResponse, this);				if (sipServerResponse != null) {					try {						sipServerResponse.processResponse(sipResponse, this);					} finally {						if (sipServerResponse instanceof SIPTransaction && 								!((SIPTransaction)sipServerResponse).passToListener())							((SIPTransaction) sipServerResponse).releaseSem();					}					// Normal processing of message.				} else {					if (sipStack.isLoggingEnabled()) {						this.sipStack.logWriter								.logDebug("null sipServerResponse!");					}				}			}			if (sipStack.threadPoolSize == -1) {				return;			}		}	}