1st: you rewrite the cookie - not ending with this code. The response object is sent once - with your "new" cookie. If a cookie exists, simply change its value and / or content. I would check your assumption on AppSettings ["cookiesecond"]
Also try the following:
If Request.ServerVariables("http_user_agent").IndexOf("Safari", StringComparison.CurrentCultureIgnoreCase) <> -1 Then
Me.Page.ClientTarget = "uplevel"
, ...