Index: pjproject/trunk/pjsip-apps/src/samples/sipecho.c
===================================================================
--- pjproject/trunk/pjsip-apps/src/samples/sipecho.c	(revision 4147)
+++ pjproject/trunk/pjsip-apps/src/samples/sipecho.c	(revision 4148)
@@ -42,8 +42,4 @@
 
 /* Settings */
-#define AF		pj_AF_INET() /* Change to pj_AF_INET6() for IPv6.
-				      * PJ_HAS_IPV6 must be enabled and
-				      * your system must support IPv6.  */
-#define SIP_PORT	5060	     /* Listening SIP port		*/
 #define MAX_CALLS	8
 
@@ -79,4 +75,8 @@
 static pj_bool_t on_rx_request( pjsip_rx_data *rdata );
 
+/* Globals */
+static int sip_af;
+static int sip_port = 5060;
+static pj_bool_t sip_tcp;
 
 /* This is a PJSIP module to be registered by application to handle
@@ -239,9 +239,4 @@
     pj_status_t status;
 
-    pj_log_set_level(5);
-
-    status = pj_init();
-    CHECK_STATUS();
-
     pj_log_set_level(3);
 
@@ -256,11 +251,16 @@
 
     pj_log_set_level(4);
-    pj_sockaddr_init(AF, &addr, NULL, (pj_uint16_t)SIP_PORT);
-    if (AF == pj_AF_INET()) {
-	status = pjsip_udp_transport_start( app.sip_endpt, &addr.ipv4, NULL,
-					    1, NULL);
-    } else if (AF == pj_AF_INET6()) {
-	status = pjsip_udp_transport_start6(app.sip_endpt, &addr.ipv6, NULL,
-					    1, NULL);
+    pj_sockaddr_init((pj_uint16_t)sip_af, &addr, NULL, (pj_uint16_t)sip_port);
+    if (sip_af == pj_AF_INET()) {
+	if (sip_tcp) {
+	    status = pjsip_tcp_transport_start( app.sip_endpt, &addr.ipv4, 1,
+						NULL);
+	} else {
+	    status = pjsip_udp_transport_start( app.sip_endpt, &addr.ipv4,
+	                                        NULL, 1, NULL);
+	}
+    } else if (sip_af == pj_AF_INET6()) {
+	    status = pjsip_udp_transport_start6(app.sip_endpt, &addr.ipv6,
+	                                        NULL, 1, NULL);
     } else {
 	status = PJ_EAFNOTSUP;
@@ -489,5 +489,5 @@
 
     /* Generate Contact URI */
-    status = pj_gethostip(AF, &hostaddr);
+    status = pj_gethostip(sip_af, &hostaddr);
     if (status != PJ_SUCCESS) {
 	app_perror(THIS_FILE, "Unable to retrieve local host IP", status);
@@ -495,5 +495,5 @@
     }
     pj_sockaddr_print(&hostaddr, hostip, sizeof(hostip), 2);
-    pj_ansi_sprintf(temp, "<sip:sipecho@%s:%d>", hostip, SIP_PORT);
+    pj_ansi_sprintf(temp, "<sip:sipecho@%s:%d>", hostip, sip_port);
     local_uri = pj_str(temp);
 
@@ -542,4 +542,15 @@
 
 
+static void usage()
+{
+    printf("\nUsage: sipecho OPTIONS\n");
+    printf("\n");
+    printf("where OPTIONS:\n");
+    printf("  --local-port, -p PORT        Bind to port PORT.\n");
+    printf("  --tcp, -t                    Listen to TCP instead.\n");
+    printf("  --ipv6, -6                   Use IPv6 instead.\n");
+    printf("  --help, -h                   Show this help page.\n");
+}
+
 /* main()
  *
@@ -549,14 +560,53 @@
 int main(int argc, char *argv[])
 {
+    struct pj_getopt_option long_options[] = {
+        { "local-port",	1, 0, 'p' },
+        { "tcp",	0, 0, 't' },
+        { "ipv6",	0, 0, '6' },
+        { "help", 	0, 0, 'h' }
+    };
+    int c, option_index;
+
+    pj_log_set_level(5);
+
+    pj_init();
+
+    sip_af = pj_AF_INET();
+
+    pj_optind = 0;
+    while ((c = pj_getopt_long(argc, argv, "p:t6h", long_options,
+                               &option_index)) != -1)
+    {
+	switch (c) {
+	case 'p':
+	    sip_port = atoi(pj_optarg);
+	    break;
+	case 't':
+	    sip_tcp = PJ_TRUE;
+	    break;
+	case 'h':
+	    usage();
+	    return 0;
+	case '6':
+	    sip_af = pj_AF_INET6();
+	    break;
+	default:
+	    PJ_LOG(1,(THIS_FILE,
+		      "Argument \"%s\" is not valid. Use --help to see help",
+		      argv[pj_optind-1]));
+	    return -1;
+	}
+    }
+
     if (init_stack())
 	goto on_error;
 
     /* If URL is specified, then make call immediately. */
-    if (argc > 1) {
+    if (pj_optind != argc) {
 	pj_sockaddr hostaddr;
 	char hostip[PJ_INET6_ADDRSTRLEN+2];
 	char temp[80];
 	call_t *call;
-	pj_str_t dst_uri = pj_str(argv[1]);
+	pj_str_t dst_uri = pj_str(argv[pj_optind]);
 	pj_str_t local_uri;
 	pjsip_dialog *dlg;
@@ -564,5 +614,5 @@
 	pjsip_tx_data *tdata;
 
-	if (pj_gethostip(AF, &hostaddr) != PJ_SUCCESS) {
+	if (pj_gethostip(sip_af, &hostaddr) != PJ_SUCCESS) {
 	    PJ_LOG(1,(THIS_FILE, "Unable to retrieve local host IP"));
 	    goto on_error;
@@ -571,5 +621,5 @@
 
 	pj_ansi_sprintf(temp, "<sip:sipecho@%s:%d>",
-			hostip, SIP_PORT);
+			hostip, sip_port);
 	local_uri = pj_str(temp);
 
