/// <summary>
 /// Create a new Port object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="iPId">Initial value of the IPId property.</param>
 /// <param name="portTypeId">Initial value of the PortTypeId property.</param>
 /// <param name="startPortNumber">Initial value of the StartPortNumber property.</param>
 /// <param name="userId">Initial value of the UserId property.</param>
 /// <param name="portDirectionId">Initial value of the PortDirectionId property.</param>
 public static Port CreatePort(global::System.Int32 id, global::System.Int32 iPId, global::System.Int32 portTypeId, global::System.Int32 startPortNumber, global::System.Int32 userId, global::System.Int32 portDirectionId)
 {
     Port port = new Port();
     port.Id = id;
     port.IPId = iPId;
     port.PortTypeId = portTypeId;
     port.StartPortNumber = startPortNumber;
     port.UserId = userId;
     port.PortDirectionId = portDirectionId;
     return port;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the Ports EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToPorts(Port port)
 {
     base.AddObject("Ports", port);
 }
        public Port CreatePort(Request request, int portId, IPAddress ipAddress, int? subnetMask, int portTypeId, int startPortNumber, int? endPortNumber, int portDirectionId, DateTime? startDate, DateTime? endDate)
        {
            if (portDirectionId == (int)PortDirections.UserToRequestedServer ||
                portDirectionId == (int)PortDirections.RequestedServerToUser)
            {
                foreach (var serverIP in serversIPs)
                {
                    if (ipAddress.ToString().StartsWith(serverIP))
                    {
                        throw new IPAddressException("Either the IPAddress is invalid or the PortDirection");
                    }
                }
            }

            var port = db.Ports.SingleOrDefault(x => x.Id == portId);
            var changed = false;

            if (port != null)
            {
                if (!port.IP.Address.SequenceEqual(ipAddress.GetAddressBytes()) || portTypeId != port.PortTypeId ||
                    startPortNumber != port.StartPortNumber || endPortNumber != port.EndPortNumber || portDirectionId != port.PortDirectionId)
                {
                    changed = true;
                }

                if (subnetMask.HasValue)
                {
                    if (port.SubnetMaskId.HasValue)
                    {
                        if (!port.SubnetMask.Address.SequenceEqual(CommonFunctions.SubnetMaskFromMaskBits(subnetMask.Value).GetAddressBytes()))
                        {
                            changed = true;
                        }
                    }
                    else
                    {
                        changed = true;
                    }
                }
                else if (port.SubnetMaskId.HasValue)
                {
                    changed = true;
                }

                if (startDate.HasValue)
                {
                    if (port.StartDate.HasValue)
                    {
                        if (!startDate.Value.Date.Equals(port.StartDate.Value))
                        {
                            changed = true;
                        }
                    }
                    else
                    {
                        changed = true;
                    }
                }
                else if (port.StartDate.HasValue)
                {
                    changed = true;
                }

                if (endDate.HasValue)
                {
                    if (port.EndDate.HasValue)
                    {
                        if (!endDate.Value.Date.Equals(port.EndDate.Value))
                        {
                            changed = true;
                        }
                    }
                    else
                    {
                        changed = true;
                    }
                }
                else if (port.EndDate.HasValue)
                {
                    changed = true;
                }
            }
            else
            {
                changed = true;
            }

            if (changed)
            {
                port = new Port
                {
                    PortTypeId = portTypeId,
                    StartPortNumber = startPortNumber,
                    EndPortNumber = endPortNumber,
                    PortDirectionId = portDirectionId,
                    IP = CreateIP(ipAddress),
                    StartDate = startDate,
                    EndDate = endDate,
                    User = GetCurrentUser()
                };

                if (port.IP.Segment == null)
                {
                    throw new IPAddressException("The IP address doesn't have a segment");
                }

                if (subnetMask.HasValue)
                {
                    port.SubnetMask = CreateSubnetMask(subnetMask.Value);
                }

                db.Ports.AddObject(port);
            }

            var requestPort = new RequestPort
            {
                Request = request,
                Port = port
            };

            db.RequestPorts.AddObject(requestPort);

            return port;
        }