title: Port Forwarding with UPnP
body: <p>I am developing a p2p chat application in delphi indy 10 . I have two questions.</p>
  
  <p>1.I am using this function for port opening . It gives Coinitialize has not been called error .i declared that but don't know whether i declared it in right place because if i run it in Xp it still gives this error.       </p>
  
  <p>2.If i am using udp hole punching then why i need to call this function? i m not able to chat if i do-not call this function.</p>
  
  <pre><code>  procedure AddPortThroughUPnP;
  
    var
    NAT     : Variant;
    Profile : Variant;
    Ports   : Variant;
  
  begin
  
    if ( not fEnableUPnP ) or ( fServerRole ) then exit;
  
    if fWindowsName = WINXP then
    begin
    try
    Coinitialize(nil);
      NAT      := CreateOleObject ( 'HNetCfg.FwMgr' );
      Profile  := NAT.LocalPolicy.CurrentProfile;
  
      if not VarIsClear ( Profile ) then
      begin
  
        Ports          := CreateOLEObject('HNetCfg.FWOpenPort');
        Ports.Name     := 'UDP Server 6002';
        Ports.Port     := 6002;
        Ports.Scope    := 0;
        Ports.Protocol := 17;
        Ports.Enabled  := True;
  
        Profile.GloballyOpenPorts.Add ( Ports );
  
      end;
    finally
       Couninitialize;
    end;
  
    end
    else
    begin
           try
     Coinitialize(nil);
      NAT   := CreateOleObject ( 'HNetCfg.NATUPnP' );
      Ports := NAT.StaticPortMappingCollection;
  
      if not VarIsClear ( Ports ) then
         Ports.Add
         ( 6002, 'UDP', fExternalPort, fLocalIP, True, 'UDP Server 6002' );
  
           finally
           Couninitialize;
           end;
    end;
  end;
  </code></pre>